Managing file sizes is essential for saving disk space and speeding up transfers. In this guide, you’ll learn how to compress and decompress files and directories using the most common Linux utilities:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
gzip, bzip2, xz, zip, and tar.
Table of Contents
- Compression Tools Overview
- Compressing and Decompressing Single Files
- Preserving Original Files
- Inspecting Compressed Archives
- Working with ZIP Archives
- Combining
tarwith Compression - References
Compression Tools Overview
Linux distributions typically include these single-file compressors by default:| Utility | File Extension | Compression Ratio | Typical Speed |
|---|---|---|---|
| gzip | .gz | Moderate | Fast |
| bzip2 | .bz2 | Better | Moderate |
| xz | .xz | Best | Slow |
Compressing and Decompressing Single Files
Use the following commands to compress and decompress individual files:By default,
gzip, bzip2, and xz delete the original file after (de)compression. Use -k or --keep to preserve input files.Preserving Original Files
To keep both the source and the compressed version, add the-k (keep) flag:
Inspecting Compressed Archives
To view metadata (compressed size, uncompressed size, compression ratio), use the--list (-l) option:
Working with ZIP Archives
Unlikegzip/bzip2/xz, the zip utility can bundle multiple files or directories into a single archive:
Combining tar with Compression
Sincegzip, bzip2, and xz operate on single files, tar is used to first archive multiple files/directories, then compress the archive.
Two-Step Archiving
One-Step Archiving with Compression
Leveragetar’s built-in compression flags:
-a) to match the extension:
Extracting Compressed Tarballs
tar will detect compression automatically: