When you download software source code, server backups, or massive datasets in Linux, they rarely arrive as a collection of loose files. Instead, they are bundled together into a single, highly compressed archive. While Windows users are accustomed to .zip files, the Linux ecosystem heavily relies on .tar.gz files (affectionately known as “tarballs”). To access the files trapped inside, you must use the command line.
What is a tar.gz File?
The .tar.gz extension actually represents two separate programs working together:
- tar (Tape Archive): This ancient Unix utility takes hundreds of separate files and directories and bundles them into a single, massive file (a
.tararchive), preserving all Linux file permissions. However,tardoes not compress the files; it just glues them together. - gzip: This program takes that massive
.tarfile and aggressively compresses it to save disk space, resulting in the final.tar.gzfile.
How to Extract (Unzip) a tar.gz File
Because these files are so common, modern versions of the tar command can handle both the decompression (gzip) and the unbundling (tar) simultaneously with a single command.
Open your terminal, navigate to the directory where your file is located, and type the following syntax:
tar -xzvf filename.tar.gz
Understanding the Flags (-xzvf)
In Linux, it is crucial to understand what you are telling the computer to do, rather than blindly copying and pasting commands. The four letters after the hyphen are flags that dictate the command’s behavior:
- -x (eXtract): This tells the
tarcommand that you want to pull files out of an archive, rather than create a new one. - -z (gZip): This tells
tarthat the archive is compressed using gzip, and it needs to decompress it first before extracting. - -v (Verbose): This makes the command “talkative.” It will print the name of every single file to the terminal screen as it extracts it, so you know the command is actually working and not frozen.
- -f (File): This is the most critical flag, and it must be the last one in the list. It tells the command that the very next word you type is the filename of the archive.
Extracting to a Specific Directory
By default, the tar -xzvf command will spill all the extracted files directly into your current working directory. If you are sitting in your cluttered /Downloads folder, this can create a mess.
If you want to extract the files into a specific, different directory (like /var/www/html/), you can append the -C (capital C for Change directory) flag to the end of the command.
tar -xzvf software_package.tar.gz -C /var/www/html/
Linux will leave the original compressed .tar.gz file untouched in your Downloads folder and place the uncompressed, extracted files cleanly into the /var/www/html/ directory.