How to Compress and Extract tar.gz Files in Linux

If you spend any time using Linux, you will inevitably encounter .tar.gz files. Often referred to as “tarballs,” these files are the Linux equivalent of a .zip file on Windows. They are heavily used by developers to distribute open-source software, source code, and system backups.

A .tar.gz file is actually the result of two separate tools working together: tar (Tape Archive), which bundles multiple files and directories into a single uncompressed file, and gzip, which compresses that single file to save space.

In this guide, we will show you how to quickly extract existing tarballs, as well as how to create your own compressed archives directly from the Linux terminal.

How to Extract (Unzip) a tar.gz File

Extracting a tarball is the most common task you will perform. The tar command can handle both the decompression (gzip) and the extraction (tar) in a single step.

  1. Open your terminal.
  2. Use the cd command to navigate to the directory containing your .tar.gz file.
  3. Run the following command, replacing filename.tar.gz with the actual name of your file:
tar -xzvf filename.tar.gz

Understanding the Command Flags:

The letters following the hyphen are crucial. Here is exactly what they tell the system to do:

  • -x (eXtract): Tells the tar command to extract files from an archive (rather than create one).
  • -z (gZip): Tells tar to decompress the archive using gzip first.
  • -v (Verbose): This is optional but highly recommended. It visually lists every file on the screen as it is extracted, so you can see the progress.
  • -f (File): This flag must always be the last one in the block. It tells tar that the very next word you type is the name of the archive file it should act upon.

Extracting to a Specific Directory

By default, the command above extracts the files into your current directory. If you want to extract them into a completely different folder, use the -C (capital C for Change directory) flag:

tar -xzvf filename.tar.gz -C /path/to/destination/folder

How to Create a New tar.gz Archive

Creating your own tarballs is an excellent way to back up directories or prepare folders for transfer to another server.

  1. Open your terminal.
  2. Run the following command structure to compress a folder:
tar -czvf name_of_new_archive.tar.gz /path/to/directory_to_compress

For example, if you have a folder called website_backup in your current directory and you want to compress it into a file called backup-2023.tar.gz, you would type:

tar -czvf backup-2023.tar.gz website_backup

Understanding the Creation Flags:

The flags are very similar to extraction, with one key difference:

  • -c (Create): This replaces the -x flag. It tells tar that you are creating a new archive from scratch.
  • -z (gZip): Compresses the new archive to save disk space.
  • -v (Verbose): Shows you which files are being added to the archive in real-time.
  • -f (File): Specifies the name of the new archive file you are creating.

Bonus: How to View the Contents of a tar.gz Without Extracting

Sometimes you download a massive tarball and want to check what is inside it before dumping hundreds of files onto your hard drive. You can list the contents without extracting anything by swapping the -x or -c flag for -t (list).

tar -tzvf filename.tar.gz

This command will output a full directory tree of the archive, allowing you to inspect the structure and ensure it is safe to extract.

Mastering these three basic tar commands gives you complete control over file archiving and compression in the Linux terminal, significantly speeding up software installation and server backups.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

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

Receive our best articles and tips delivered straight to your inbox.