When you download software source code or backups in a Linux environment, they are almost exclusively distributed in the `.tar.gz` format. Often referred to as “tarballs,” these files are the Linux equivalent of a `.zip` file. Understanding how to extract tar.gz files in Ubuntu is a fundamental command-line skill that every Linux user must master.
A tar.gz file is actually a combination of two utilities: `tar` binds multiple files together into one archive, and `gzip` compresses that archive to save disk space. Here is the safest and most efficient way to handle them using the terminal.
How to Extract a TAR.GZ File
To extract (or “unzip”) a tarball, you will use the standard `tar` command followed by a series of flags that tell the system exactly what to do.
- Open your terminal by pressing Ctrl + Alt + T.
- Navigate to the folder containing your file using the
cdcommand (e.g.,cd ~/Downloads). - Run the following command to extract the archive:
tar -xvzf filename.tar.gz
Understanding the Flags
It is important to understand what those four letters (xvzf) are doing so you can troubleshoot if something goes wrong:
- x (eXtract): Tells tar to extract the files from the archive.
- v (Verbose): Prints the names of the files to your screen as they are extracted, letting you track the progress.
- z (gZip): Tells tar to decompress the gzip compression simultaneously.
- f (File): Specifies the filename of the archive you want to process. (This must always be the last flag before the filename).
How to Extract Files to a Specific Directory
By default, the command above extracts the files directly into your current working directory. If you want to neatly unpack the archive into a different folder (for example, `/opt/software`), you can use the `-C` (Change directory) flag.
- Run the command with the appended flag:
tar -xvzf filename.tar.gz -C /path/to/destination/
Note: If you are extracting files to a protected system directory like `/opt`, you will need to add sudo to the beginning of the command to grant administrator privileges.
While modern Ubuntu graphical interfaces allow you to right-click and extract these archives, using the terminal is significantly faster and more reliable, especially when managing servers over SSH. Now that you know how to extract tar.gz files in Ubuntu, you can confidently install software packages and manage compressed backups like a seasoned Linux administrator.