gzip, bzip2, xz, zip, and tar. Mastering these tools helps you save storage space and speed up file transfers across systems.
Table of Contents
- Compression Utilities Overview
- Basic Compression with gzip, bzip2, and xz
- Keeping Original Files
- Creating and Extracting zip Archives
- Combining tar with Compression
- Links and References
1. Compression Utilities Overview
Most Linux distributions include three primary compression tools by default:| Utility | File Extension | Compress Command | Decompress Command |
|---|---|---|---|
| gzip | .gz | gzip file | gunzip file.gz |
| bzip2 | .bz2 | bzip2 file | bunzip2 file.bz2 |
| xz | .xz | xz file | unxz file.xz |
2. Basic Compression with gzip, bzip2, and xz
To compress a single file:--decompress (or -d) flag:
By default, these commands remove the original file after compressing or decompressing.
3. Keeping Original Files
To retain the input files alongside the compressed versions, add the-k (or --keep) flag:
--list (or -l) option:
4. Creating and Extracting zip Archives
Thezip utility bundles and compresses files into a single archive. To compress individual files:
Pictures/):
.zip archive with unzip:
gzip, bzip2, and xz, the zip format natively supports multiple files and directories in one archive.
5. Combining tar with Compression
To package multiple files or directories and compress them in one step, usetar together with a compression option.
5.1 Creating a Tar Archive, Then Compressing
- Create a tarball:
- Compress it:
5.2 One-Step Creation and Compression
Use the appropriate flag to combine creation and compression:| Compressor | Short Option | Command Example |
|---|---|---|
| gzip | -z | tar -czf archive.tar.gz file1 dir/ |
| bzip2 | -j | tar -cjf archive.tar.bz2 file1 dir/ |
| xz | -J | tar -cJf archive.tar.xz file1 dir/ |
| auto | --auto-compress or -a | tar -caf archive.tar.gz file1 dir/ |
5.3 Extracting Compressed Tarballs
tar auto-detects compression, so extracting is straightforward:
Overwriting existing files when extracting archives can lead to data loss. Always verify the contents before extraction or use
--list (-t) to preview: