AI Hints
Home›Linux Tutorial›Archives
Linux · Lesson 89 of 120

Compressing Files

Gzip and zip.

⏱ 2 min read▶ Video at 1:59:01🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

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.

Compressing Files
From the video: Compressing Files
  • 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.

Terminal
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.

Terminal
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.

keyvalue
tar -czf x.tar.gzLinux, backups
tar -xzf x.tar.gzextract it
zip -r x.zip folderfor Windows users
Text shrinksmedia 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
Next up: Practice: A Dated Backup — tar plus date