How to Compress Directories in Linux: A Comprehensive Guide

February 27, 2023
Compressing directories in Linux is an essential task that every user should master. Here, we will see how to do it quickly and efficiently.
Quick Example of Compressing and Decompressing
To compress a directory:
tar -czvf my_directory.tar.gz /path/to/directory
To decompress:
tar -xzvf my_directory.tar.gz
Description of the Commands Used
tar
The tar (Tape Archive) command is one of the most used for bundling and compressing files in Linux. Despite its name suggesting its origin associated with backup tapes, today it is a universal tool for file manipulation.
-c: Create a new tar archive.-z: Compress the archive with gzip.-v: Show the progress in the command line, i.e., verbose output.-f: Specify the name of the tar archive to create or extract.
For example, the command tar -czvf my_directory.tar.gz /path/to/directory performs the following actions:
-c: Create a new tar archive.-z: Compress it using gzip.-v: Show details of the files added to the tar.-f my_directory.tar.gz: The resulting file will bemy_directory.tar.gz.
gzip
gzip is a utility that reduces the size of files using the Lempel-Ziv (LZ77) compression algorithm. It works closely with tar to offer a complete compression solution.
To decompress the created tar.gz file, we use:
tar -xzvf my_directory.tar.gz
-x: Extract the files from the tar archive.-z,-v,-f: The same options already described.
Summary of the Most Important Commands
- Compress a directory:
tar -czvf archive.tar.gz /path/to/directory - Decompress an archive:
tar -xzvf archive.tar.gz - Common tar Options:
-c: Create a tar archive.-x: Extract contents from a tar archive.-z: Compress/decompress using gzip.-v: Verbose, show details during the operation.-f: Specify the archive file name.
Brief History of tar and Its Preference in Linux
The tar command was introduced in the early days of Unix in the 1970s. Originally designed for writing data to backup magnetic tapes (hence its name, Tape Archive), tar quickly became a general tool for bundling and manipulating files. Its simple and efficient design made it ideal for Unix systems and later for Linux.
Although zip is also a popular utility for file compression, tar has been preferred in the Linux community for several reasons:
- Compatibility and Portability:
taris an integral part of almost all Unix and Linux distributions, ensuring that any script or tool using it will work on a wide variety of systems. - Flexibility:
tarcan be combined with many other tools such asgzipandbzip2to achieve different levels and types of compression. - Efficiency in Handling Large Files:
tarmore effectively manages large sets of files and directories without the overhead of intermediate file systems thatzipmight introduce. - Use in Automation Scripts: The ease of use of
tarin combination with shell scripts makes it ideal for automated backup and deployment tasks.
In summary, tar remains a fundamental tool in the Linux ecosystem due to its history, versatility, and efficiency.