Compress and Extract Files Quickly on Linux
Linux provides several standard compression tools. Some compress a single file, while tar combines many files into one archive and can apply compression at the same time.
gzip
gzip filename
gunzip filename.gzgzip is widely available and usually a good balance between speed and compression ratio.
bzip2
bzip2 filename
bunzip2 filename.bz2bzip2 can compress more strongly than gzip for some data, but it is generally slower.
xz
xz filename
unxz filename.xzxz often achieves strong compression, especially for distributable archives, at the cost of more CPU time.
Archive Directories with tar
Create an uncompressed archive:
tar -cf archive.tar directory/Create compressed archives:
tar -czf archive.tar.gz directory/ # gzip
tar -cjf archive.tar.bz2 directory/ # bzip2
tar -cJf archive.tar.xz directory/ # xzExtract them:
tar -xf archive.tar
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2
tar -xJf archive.tar.xzAdd -v when you want verbose filenames printed during the operation.
ZIP Archives
ZIP is useful when archives need to move easily between Linux, macOS, and Windows:
zip -r archive.zip directory/
unzip archive.zipList an Archive Before Extracting
tar -tf archive.tar.gz
unzip -l archive.zipInspecting unknown archives first can help avoid unexpected filenames or directory layouts.
Choosing a Format
- Use gzip for widespread compatibility and fast compression.
- Use xz when smaller archive size is more important than compression speed.
- Use
tarwhen combining many files or directories. - Use ZIP when cross-platform convenience matters.
Conclusion
Compression on Linux is mostly a choice between archive structure, compatibility, speed, and compression ratio. Learn the common tar, gzip, xz, and ZIP commands and choose the format that matches the transfer or storage requirement.