📒 

A .tar.gz file is a compressed archive that combines two types of files: a .tar file (Tape Archive) and a .gz (gzip) compressed file. It is commonly used for packaging multiple files into a single archive and compressing them to save space. Extracting .tar.gz files is a common task in Linux, and this guide will show you how to do it using the command line.

Basic Syntax for Extracting .tar.gz Files

To extract a .tar.gz file, use the tar command, which stands for tape archive. The basic syntax is:

tar -xzvf archive-name.tar.gz

Here’s what each option means:

  • -x: Extract the files from the archive.
  • -z: Use gzip to decompress the .gz file.
  • -v: Verbose output, which lists the files being extracted.
  • -f: Specifies the archive file name.

Step-by-Step Instructions

  1. Open Terminal: Press Ctrl + Alt + T on your keyboard to open the terminal on most Linux systems.
  2. Navigate to the Directory: Change to the directory where your .tar.gz file is located using the cd command:
    cd /path/to/directory
  3. Extract the .tar.gz File: Run the following command to extract the contents:
    tar -xzvf archive-name.tar.gz

    Replace archive-name.tar.gz with the name of your file.

Example:

If you have a file called sample-archive.tar.gz in your Downloads folder, you can extract it using:

cd ~/Downloads
tar -xzvf sample-archive.tar.gz

Extracting to a Specific Directory

To extract the contents of a .tar.gz file into a specific directory, use the -C option followed by the target directory path:

tar -xzvf archive-name.tar.gz -C /path/to/destination

This command will extract the contents of archive-name.tar.gz into the specified destination folder.

Example:

If you want to extract sample-archive.tar.gz into a directory named my-files in your home folder:

mkdir ~/my-files
tar -xzvf sample-archive.tar.gz -C ~/my-files

Extracting .tar.gz Files Without Verbose Output

If you prefer not to see the list of files being extracted, you can omit the -v option:

tar -xzf archive-name.tar.gz

This command will still extract the files but without displaying them in the terminal.

Viewing Contents Without Extracting

To list the contents of a .tar.gz file without extracting them, use the -t option:

tar -tzvf archive-name.tar.gz

This command shows you the files and directories contained in the archive without extracting them.

Extracting .tar Files (Without .gz Compression)

If you have a .tar file (without .gz compression), you can extract it using:

tar -xvf archive-name.tar

The -z option is not needed since the file is not compressed with gzip.

Conclusion

Extracting .tar.gz files using the Linux command line is straightforward with the tar command. Remember to use the -xzvf options to extract and view the process, and use -C to specify a different destination. Whether you’re managing backups, software packages, or other compressed data, mastering these commands will make handling .tar.gz files much easier.