If you download source code, software packages, or backup files in a Linux environment, you will inevitably encounter files ending in .tar.gz. To a Windows user accustomed to standard .zip files, these double-barreled extensions can look intimidating. However, they are simply the Linux equivalent of a zipped folder.
The .tar.gz extension actually represents two completely different technologies working together:
- TAR (Tape Archive): This tool takes dozens of individual files and folders and sticks them all together into one massive, solid block (a “tarball”). However, it does not compress them; the resulting file is exactly the same size as the original files.
- GZ (Gzip): This tool takes that single, massive tarball block and aggressively compresses it to save disk space.
While you can technically unpack these two layers separately, the modern Linux command line allows you to extract the entire package in a single, simple command.
How to Extract a .tar.gz File
To extract the contents of a tarball, you will use the incredibly powerful tar command, followed by a specific string of four flags.
- Open your terminal application.
- Use the
cdcommand to navigate to the directory where your downloaded file is located (e.g.,cd ~/Downloads). - Type the following command, replacing filename with the actual name of your file:
tar -xzvf filename.tar.gz
Press Enter, and the terminal will instantly spit out a scrolling list of every file as it is unpacked. The files will be placed into a new folder in your current directory.
What Do the Flags Mean?
The secret to mastering the tar command is understanding what those four letters (-xzvf) actually instruct the computer to do. You must type them exactly, though the order of the first three does not matter.
- x (Extract): This is the core instruction. It tells the tar program that you want to extract files out of an archive, rather than create (c) a new one.
- z (Gzip): This tells the program that the file has been compressed using Gzip technology, instructing it to decompress the file before trying to unpack it. (If you ever encounter a file that is just
.tarwithout the.gz, you would omit this flag). - v (Verbose): This makes the command “loud.” It tells the terminal to print the name of every single file on the screen as it is extracted. If you omit the ‘v’, the command will run silently in the background and simply return you to the prompt when finished.
- f (File): This flag is critical and must always be the absolute last letter in the string. It tells the command that the very next word you type is the filename of the archive. If you type
-fxzv, the command will fail because it will look for a file named “xzv”.
How to Extract to a Different Directory
By default, the tar command dumps all the extracted files right at your feet, in whatever directory you are currently standing in. If you download a file to your ~/Downloads folder but you want the extracted files to be placed neatly into /opt/software, you do not need to move the file first.
You can use the capital -C (Change directory) flag to specify a destination.
sudo tar -xzvf filename.tar.gz -C /opt/software
Note: Because we are extracting files into the protected /opt system directory in this example, we must prepend the command with sudo to grant it administrative write privileges.