How To Unzip A tar.gz File

Utsav Poudel
Utsav Poudel
Published: December 12, 2023

The short answer

To unzip a tar.gz file on Linux, macOS, and Windows, you can use the tar command with the -xvzf flags as follows:

Bash
$ tar -xvzf <archive>

Where:

  • The -x flag is used to extract the content of the archive.
  • The -v flag is used to output the list of files being extracted.
  • The -z flag is used to decompress the file using gzip.
  • The -f flag is used to specify the path of the archive file.
  • archive is the path to the tar.gz file you want to unzip.

For example:

Bash
$ tar -xvzf ~/Downloads/documents.tar.gz

Unzipping a tar.gz file into a specific folder

To extract the files contained in a tar.gz archive to a specific folder, you can use the -C flag as follows:

Bash
$ tar -xf <archive> -C <folder>

Where:

  • archive is the path to the tar.gz file to unzip.
  • folder is the path to the target directory to extract the files into.

For example:

Bash
$ tar -xf archive.tar.gz -C /home/user/desktop

This command extracts the contents of the archive.tar.gz file and places them in the /home/user/desktop directory.

Easily retrieve this command using Warp’s AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature

Entering tar unzip to specific folder in the AI Command Suggestions will prompt a tar command that can then quickly be inserted into your shell by doing CMD+ENTER.

Listing the contents of a tar.gz archive

To list the contents of a tar.gz archive without actually extracting it, you can use the -t flag as follows:

Bash
$ tar -tf <archive>

Where:

  • The -t flag is used to print the list of filenames contained in the archive.

Extracting specific files from a tar.gz archive

To extract specific files from a tar.gz archive, you can use the tar command with the --wildcards flag as follows:

Bash
$ tar -xf <archive> --wildcards '<pattern>'

Where:

  • pattern is a shell globbing pattern like \.js or log?.txt.

For example:

Bash
$ tar -xf documents.tar.gz --wildcards '*.pdf'

This command will only extract the files with a .pdf extension from the .tar.gz archive.

Note that some versions of the tar command, particularly on macOS, might not support the --wildcards flag. In such cases, you can directly specify the globbing pattern after the command as follows:

Bash
$ tar -xf <archive> “<pattern>”

Unzipping multiple tar.gz archives

To unzip multiple tar.gz archives located in the same directory at once, you can use the following command:

Bash
$ for file in *.tar.gz; do tar -xzf "$file"; done

Where:

  • for file in \.tar.gz is used to create a loop that iterates over all the files with a .tar.gz extension in the current directory.
  • do tar -xzf "$file"; is used to execute the tar command on each of these files.

Note that this loop will extract the contents of the .tar.gz files in the current directory.

Downloading and unzipping using Wget

To download and extract a tar.gz file with Wget, you can redirect the output of the wget command to the tar command using the pipe operator as follows:

Bash
$ wget -c <URL> -O - | tar -xz

Where:

  • wget -c is used to download files from the specified URL.
  • -O - is used to specify that wget should write the content of the downloaded files to the standard output.

Note that if you are extracting files to a certain directory that requires root permissions, then you might have to execute this command using sudo.

Unzipping a tar.gz file using a Python script

To extract a tar.gz file with Python, you can use the tarfile module as follows:

Bash
import tarfile

file_name = 'path/to/the/archive'
destination_folder = 'path/to/the/folder'

file = tarfile.open(file_name)
file.extractall(destination_folder)
file.close()

Unzipping tar.gz files using GUI and online alternatives

If you find difficulties working with command-line, know that you can also use a desktop app or online alternative to extract a tar.gz file. On the desktop, you can use apps like 7 zip or WinRAR. In the browser, you can use websites like ezyZip.

Written by
Utsav Poudel
Utsav Poudel
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.