Skip to Content

How to Extract or Unzip tar GZ File in Linux using Command Line

Learn how to extract or unzip tar.gz files in Linux using the tar command and other tools. Find out how to list, extract, and create tar.gz archives.

A tar.gz file is a compressed archive format commonly used in Linux systems to combine multiple files and directories into a single file while reducing their size. It combines the tar utility for archiving and the gzip utility for compression. Knowing how to extract or unzip a tar.gz file allows users to access and manipulate the archives’ contents efficiently.

In this article, you will learn how to extract or unzip tar.gz files in Linux using the tar command and other tools. You will also learn how to list the contents of a tar.gz file and how to create your own tar.gz archives.

How to List Contents of a tar.gz File in Linux

Before extracting a tar.gz file, you may want to see what files and directories it contains. To do that, you can use the tar command with the -t option, which lists the contents of an archive without extracting it. You also need to use the -z option to tell tar that the archive is compressed with gzip, and the -f option to specify the file name.

For example, to list the contents of a tar.gz file named archive.tar.gz, you can use the following command:

tar -ztvf archive.tar.gz

How to Extract or Unzip tar GZ File in Linux using Command Line

The -v option makes the tar command more verbose and prints the details of each file, such as permissions, owner, size, and modification date. If you only want to see the file names, you can omit the -v option.

The output of the command will look something like this:

-rw-r--r-- linuxize/linuxize    1024 2024-02-23 11:38 file1.txt
-rw-r--r-- linuxize/linuxize    2048 2024-02-23 11:39 file2.txt
drwxr-xr-x linuxize/linuxize       0 2024-02-23 11:40 dir1/
-rw-r--r-- linuxize/linuxize    4096 2024-02-23 11:41 dir1/file3.txt

How to Extract or Unzip a tar.gz File in Linux

There are several ways to extract or unzip a tar.gz file in Linux, depending on the tool you have available or prefer to use. The most common and widely supported method is to use the tar command, which can handle various compression formats, including gzip.

How to Extract or Unzip a tar.gz File in Linux using tar

To extract a tar.gz file using the tar command, you need to use the -x option, which tells tar to extract the files from the archive. You also need to use the -z option to indicate that the archive is compressed with gzip, and the -f option to specify the file name.

The general syntax for extracting a tar.gz file using tar is:

tar -xzf archive.tar.gz

For example, to extract a tar.gz file named archive.tar.gz, you can use the following command:

tar -xzf archive.tar.gz

The command will extract the files and directories in the current working directory, preserving the original permissions and ownership.

If you want to see the progress of the extraction, you can use the -v option, which makes the tar command more verbose and prints the names of the files being extracted.

tar -xzvf archive.tar.gz

By default, tar extracts the archive contents in the current working directory. If you want to extract the files to a different directory, you can use the -C option, which tells tar to change the directory before extracting the files. You need to specify the path to the directory where you want to extract the files after the -C option.

For example, to extract the archive contents to the /home/linuxize/files directory, you can use the following command:

tar -xzf archive.tar.gz -C /home/linuxize/files

If the directory does not exist, tar will create it automatically.

You can also extract only specific files or directories from a tar.gz archive, by providing their names after the archive name. You need to use the exact names and paths as listed by the tar -t command.

For example, to extract only the file file1.txt and the directory dir1 from the archive.tar.gz file, you can use the following command:

tar -xzf archive.tar.gz file1.txt dir1

How to Extract or Unzip a tar.gz File in Linux using GUI Tools

If you prefer to use a graphical user interface (GUI) instead of the command-line interface (CLI), you can use your file manager or a dedicated archive manager to extract or unzip tar.gz files.

Most file managers, such as Nautilus, Dolphin, or Thunar, support extracting tar.gz files by right-clicking on the file and choosing the Extract option from the context menu. You can also choose the destination directory where you want to extract the files.

Some of the most popular archive managers for Linux are File Roller, Ark, Xarchiver, and PeaZip. They offer more features and options than the file managers, such as creating, modifying, encrypting, and splitting archives. You can install them from your distribution’s package manager or software center.

To extract a tar.gz file using an archive manager, you need to open the file with the archive manager and then click on the Extract button. You can also select the files and directories you want to extract and the destination directory where you want to extract them.

How to Create a tar.gz File in Linux

If you want to create your own tar.gz file, you can use the tar command with the -c option, which tells tar to create a new archive. You also need to use the -z option to compress the archive with gzip, and the -f option to specify the file name.

The general syntax for creating a tar.gz file using tar is:

tar -czf archive.tar.gz file_or_directory

You can specify one or more files or directories to include in the archive. If you specify a directory, tar will recursively archive all the files and subdirectories in that directory.

For example, to create a tar.gz file named archive.tar.gz that contains the files file1.txt and file2.txt and the directory dir1, you can use the following command:

tar -czf archive.tar.gz file1.txt file2.txt dir1

If you want to see the progress of the compression, you can use the -v option, which makes the tar command more verbose and prints the names of the files being added to the archive.

tar -czvf archive.tar.gz file1.txt file2.txt dir1

Conclusion

A tar.gz file is a compressed archive format commonly used in Linux systems. It combines the tar utility for archiving and the gzip utility for compression.

In this article, you learned how to extract or unzip tar.gz files in Linux using the tar command and other tools. You also learned how to list the contents of a tar.gz file and how to create your own tar.gz archives.

To learn more about the tar command and its options, you can check the tar man page or type tar –help in your terminal.