Compressing Files
Gzip and zip.
A tar file is just files packed together. It's not any smaller. To save space, you compress it. And there's a letter for that, so tar can do both jobs at once.

- tar packs, but doesn't shrink
- Compression saves space
- tar can do both
Tar and gzip together
Add Z to the letters, and tar compresses with gzip, as it packs. By tradition, the file ends in dot tar dot GZ. Our logs folder is about two hundred kilobytes. Compressed, it's tiny, because log files are very repetitive. To extract, use dash XZF.
du -sh logs 204K logs tar -czf logs.tar.gz logs ls -lh logs.tar.gz -rw-r--r-- 1 sam sam 921 Jan 5 09:00 logs.tar.gz
Gzip on its own, and zip
Gzip on its own compresses a single file, and replaces it with a dot GZ version. Gunzip turns it back. And for sharing with Windows users, zip and unzip are often friendlier, because Windows opens zip files directly.
gzip logs/server.log && ls logs big.log server.log.gz gunzip logs/server.log.gz && ls logs big.log server.log zip -rq pictures.zip Pictures ls -l pictures.zip -rw-r--r-- 1 sam sam 582 Jan 5 09:00 pictures.zip
Which format?
Use dot tar dot GZ for Linux to Linux, and for backups. It keeps permissions and owners. Use zip when sharing with Windows or Mac users. Photos, videos, and music are already compressed, so they won't shrink much. Text, logs, and code shrink a lot.
| key | value |
|---|---|
| tar -czf x.tar.gz | Linux, backups |
| tar -xzf x.tar.gz | extract it |
| zip -r x.zip folder | for Windows users |
| Text shrinks | media doesn't |
Summary
Tar with Z compresses as it packs, gzip squeezes single files, and zip is best for sharing with Windows. Next, a backup project.
Key takeaways- tar -czf
- gzip / gunzip
- zip -r / unzip