Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Compress and Extract Files Quickly on Linux

1 min read .
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.gz

gzip is widely available and usually a good balance between speed and compression ratio.

bzip2

bzip2 filename
bunzip2 filename.bz2

bzip2 can compress more strongly than gzip for some data, but it is generally slower.

xz

xz filename
unxz filename.xz

xz 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/   # xz

Extract them:

tar -xf archive.tar
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2
tar -xJf archive.tar.xz

Add -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.zip

List an Archive Before Extracting

tar -tf archive.tar.gz
unzip -l archive.zip

Inspecting 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 tar when 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.

Related Posts

chevron-up