Learn to compress and uncompress files in Linux using utilities like gzip, bzip2, xz, and zip for efficient file management.
In this article, you will learn how to compress and uncompress files in Linux. File compression reduces storage space and speeds up file transfers between systems. Most Linux distributions come with at least three built-in compression utilities: gzip, bzip2, and xz.
Below is a simple example of compressing files with gzip and bzip2:
Copy
Ask AI
$ gzip file1$ bzip2 file2
When these commands are executed, each file is compressed into a new file (e.g., file1 becomes file1.gz and file2 becomes file2.bz2) and the original file is automatically deleted.
Sometimes you may want to compress a file without deleting the original. Most Linux compression utilities offer an option to keep the original file. Use the --help flag to check available options:
Copy
Ask AI
$ gzip --helpUsage: gzip [OPTION]... [FILE]...Compress or uncompress FILEs (by default, compress FILES in-place).Mandatory arguments to long options are mandatory for short options too. -c, --stdout write on standard output, keep original files unchanged -d, --decompress decompress -f, --force force overwrite of output file and compress links -h, --help give this help -k, --keep keep (don't delete) input files -l, --list list compressed file contents -L, --license display software license -N, --no-name do not save or restore the original name and timestamp -n, --name save or restore the original name and timestamp -q, --quiet suppress all warnings -r, --recursive operate recursively on directories --rsyncable make rsync-friendly archive -S, --suffix=SUF use suffix SUF on compressed files --synchronous synchronous output -t, --test test compressed file integrity -v, --verbose verbose mode -V, --version display version number
Using the --keep (or -k) option retains the original file when compressing.
While gzip, bzip2, and xz are popular for compressing individual files, the zip utility can compress an entire directory or multiple files into a single archive.
Unlike zip, utilities like gzip do not support compressing multiple files into a single archive. Instead, tar is commonly used to bundle files together before compression.
$ tar --create --file archive.tar file1$ gzip archive.tar# This creates archive.tar.gz
If you want to keep the uncompressed tar archive along with the compressed file, use the --keep option:
Copy
Ask AI
$ gzip --keep archive.tar# This results in both archive.tar and archive.tar.gz
Modern versions of tar allow you to create and compress an archive in a single step. The compression utility is automatically selected based on the file extension:
Copy
Ask AI
$ tar --create --gzip --file archive.tar.gz file1$ tar --create --bzip2 --file archive.tar.bz2 file1$ tar --create --xz --file archive.tar.xz file1$ tar --create --autocompress --file archive.tar.gz file1
During extraction, tar automatically detects the compression type, eliminating the need to specify a decompression method.
This article provided an overview of various techniques for compressing and uncompressing files in Linux using utilities such as gzip, bzip2, xz, and zip. You also learned how to use tar for archiving and compressing multiple files. These methods help efficiently manage file sizes and streamline file transfers.For more detailed information on Linux file management and compression techniques, explore additional resources like Kubernetes Basics or visit Linux Documentation.