Skip to main content
In this guide, we explore various methods to compress and decompress files on Linux. Compression not only saves disk space but also enhances file transfer speeds between systems. Most Linux distributions come with several built-in compression utilities. The three most common are gzip, bzip2, and xz. Each of these tools offers a simple command-line interface.
Using compression can significantly reduce file transfer times over slow networks.

Basic Compression Techniques

For example, to compress a file using gzip, run:
This command compresses file1 and produces a new file named file1.gz while automatically deleting the original file. Similarly, you can compress other files with bzip2 or xz:
To decompress these files, you can use the corresponding utilities:
By default, these commands delete the original uncompressed file. If you need to retain the source file, use the -k or --keep option.

Preserving Original Files

To prevent the deletion of the original files after compression, use the --keep option. This option is available in gzip, bzip2, and xz. For example:
You can view all the available options for a utility by running the --help command. For instance, to display gzip’s help information:
Additionally, you can list information about a compressed file using the --list option with gzip:

Archiving with ZIP and TAR

While gzip, bzip2, and xz are primarily focused on compressing a single file, the zip utility is capable of both archiving and compressing files. To create an archive containing file1, run:
To compress an entire directory such as “Pictures” and include all subdirectories recursively, use the -r option:
It is important to note that gzip and similar utilities cannot archive multiple files into a single compressed file. In such cases, the tar utility is used to create an archive first, and then the archive is compressed with your chosen tool.

Creating and Compressing Tar Archives

To create a simple tar archive containing file1:
After creating the tar archive, you can compress it with gzip:
If you wish to retain the original tar file, use:
Tar can also combine the archiving and compression operations in a single step. For example, to create a gzip-compressed tar archive:
Similarly, for bzip2 and xz compression:
Tar also offers an auto-compression mode that automatically selects the compression tool based on the file extension:
A convenient alias for creating compressed archives with tar is:

Extracting Compressed Archives

To extract files from a compressed tar archive, tar will automatically detect the compression method used based on the file extension. For example, to extract a gzip-compressed archive:
Or using the abbreviated command:

Conclusion

This guide covered the essential commands for compressing and decompressing files on Linux, from simple file compression with gzip, bzip2, and xz to the advanced usage of tar for archiving and compression. Using these techniques will help manage disk space efficiently and improve file handling performance. For more detailed information, consider exploring the Linux Documentation and other related resources. We’ll see you in the next lesson!

Watch Video