How to Extract a Tar.gz File in Linux

If you have spent your entire life using Windows, you are probably familiar with .zip files. In the Linux ecosystem, the equivalent standard is the .tar.gz file (affectionately known as a “tarball”). Almost every open-source software package, source code repository, or server backup you download from the internet will arrive in this format.

A .tar.gz file is actually two different technologies stacked on top of each other. First, the tar (Tape Archive) utility bundles 50 different files together into one single, massive block. Second, the gzip utility compresses that block to make the file size smaller. To open it, you must use the terminal to unpack it.

Method 1: The Standard Extraction Command

You can extract the entire contents of a tarball with a single, highly standardized command. You do not need to install any extra software; the tar utility is baked into every Linux distribution on earth.

  1. Open your terminal and navigate to the directory where your downloaded file lives.
  2. Type this command (replacing “filename.tar.gz” with the actual name of your file):

tar -xzvf filename.tar.gz

  1. Press Enter.

The terminal will instantly vomit a massive list of file names down the screen as it rapidly extracts them. When the prompt returns, type ls to list the directory contents. You will see a brand new folder containing all of your extracted files.

What Do Those Flags Actually Mean?

In Linux, the letters following the dash are called “flags,” and they instruct the tool on exactly how to behave. It is crucial to understand what you just typed:

  • -x (eXtract): This is the core command. It tells the tool to unpack an archive, rather than create a new one.
  • -z (gZip): This tells the tool that the archive was compressed using the gzip algorithm, so it must decompress it first. (If your file is just a .tar without the .gz on the end, you must remove this flag).
  • -v (Verbose): This forces the tool to print the name of every single file it extracts to the screen so you can watch the progress. If you are extracting a million files and want it to happen silently, remove this flag.
  • -f (File): This tells the tool that the very next word you type will be the target filename. (Note: The ‘f’ must always be the absolute last letter in the sequence. -xfvz will break the command).

Method 2: Extract to a Specific Directory

By default, the tar command unpacks the files directly into whatever folder you are currently standing in. If you are standing in your ~/Downloads folder, but you want to extract a web server directly into your /var/www/ directory, you do not have to extract it and then move it manually.

You can use the capital -C flag (Change Directory) to dictate exactly where the files should go.

  1. Type this command:

sudo tar -xzvf filename.tar.gz -C /var/www/html/

  1. Press Enter.

The tarball will remain safely in your Downloads folder, but all the extracted files will instantly materialize inside the /var/www/html/ directory.

Method 3: Extract Only One Specific File

If you downloaded a massive 10GB database backup, but you only need to retrieve a single, tiny config.txt file from inside it, you do not have to extract the entire 10GB archive and crash your hard drive. You can extract a single file surgically.

Step 1: Look Inside the Archive

First, you must list the contents of the archive so you know the exact, case-sensitive file path of the item you want. Use the -t (list) flag instead of the -x (extract) flag.

tar -tzvf filename.tar.gz

Scroll through the printed list and find the exact path (e.g., backup/settings/config.txt).

Step 2: Extract the Target File

Now, run the standard extraction command, but add the specific file path to the absolute end of the command.

tar -xzvf filename.tar.gz backup/settings/config.txt

The terminal will instantly spit out that single file and leave the rest of the 10GB archive completely untouched.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.