When downloading software, source code, or system backups in Linux, you will rarely encounter standard .zip files. Instead, the Linux ecosystem relies heavily on “tarballs.” These are files ending in .tar.gz (or occasionally just .tgz).
This file extension represents a two-step process. First, the tar (Tape Archive) utility bundles multiple files and directories together into one large, uncompressed file. Second, the gzip utility compresses that massive tar file to save disk space and reduce download times.
Because these are two distinct operations combined into one file type, users transitioning from Windows or macOS often struggle with how to open them. However, you do not need two separate tools to unpack them; modern Ubuntu distributions can handle both operations simultaneously using a single command in the terminal.
This guide explains how to extract a .tar.gz archive using the Ubuntu command line.
The Standard Extraction Command
To extract the contents of a tarball into your current directory, you use the tar command followed by four specific flags (options) that tell the system exactly how to handle the file.
Open your terminal and use the following syntax, replacing filename.tar.gz with the actual name of your downloaded file:
tar -xzvf filename.tar.gz
Understanding the Flags
It is important to understand what those four letters actually do, rather than just blindly copying and pasting them:
- -x (Extract): This is the primary instruction. It tells the
tarutility that you want to extract data out of an existing archive, rather than create a new one. - -z (Gzip): This flag tells the utility that the archive is compressed using gzip. It instructs the system to decompress the file before attempting to extract the contents. (If you were extracting a
.tar.bz2file, you would use-jinstead of-z). - -v (Verbose): This stands for verbose output. It forces the terminal to print the name of every single file to the screen as it is being extracted. This is highly recommended so you can verify the process is actually working and not just hanging.
- -f (File): This flag must always be the very last letter in the sequence. It tells the command that the text immediately following it is the name of the file you want to operate on.
Extracting to a Specific Directory
By default, the tar -xzvf command will dump all the extracted files directly into whichever folder your terminal is currently sitting in. If the archive contains 500 loose files, it will make a massive mess of your current directory.
To keep things organized, you can tell the command to extract the contents into a completely different, specific folder by adding the -C (Capital C) flag to the end of the command.
For example, to extract a file located in your Downloads folder directly into the /opt/ directory, you would run:
sudo tar -xzvf ~/Downloads/software.tar.gz -C /opt/
(Note: We use sudo in this example because the /opt/ directory requires root privileges to write to. If you are extracting to a folder you own, like ~/Documents, you do not need sudo).
How to View the Contents Without Extracting
If you downloaded a large archive from an untrusted source, it is good practice to peek inside the file before actually extracting it to your hard drive.
You can list the contents of the archive without unpacking it by replacing the -x (extract) flag with the -t (list) flag:
tar -tzvf filename.tar.gz
This command will output a detailed list of every file, folder, and file permission contained within the archive, allowing you to verify its contents safely.