Terminus
How To Unzip A tar.gz File

How To Unzip A tar.gz File

The short answer

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

 $ tar -xvzf <archive>

Where:

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

For example:

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

[#unzip-a-tar-gz-file-in-a-specific-folder] Unzipping a tar.gz file into a specific folder [#unzip-a-tar-gz-file-in-a-specific-folder]

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

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

Where:

  • [.inline-code]archive[.inline-code] is the path to the [.inline-code]tar.gz[.inline-code] file to unzip.
  • [.inline-code]folder[.inline-code] is the path to the target directory to extract the files into.

For example:

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

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

[#easily-recall-this-syntax-using-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-this-syntax-using-ai]

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

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

[#list-the-contents-of-tar-gz-files] Listing the contents of a tar.gz archive [#list-the-contents-of-tar-gz-files]

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

 $ tar -tf <archive>

Where: 

  • The [.inline-code]-t[.inline-code] 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 [.inline-code]tar.gz[.inline-code] archive, you can use the [.inline-code]tar[.inline-code] command with the [.inline-code]--wildcards[.inline-code] flag as follows:

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

Where:

  • [.inline-code]pattern[.inline-code] is a shell globbing pattern like [.inline-code]*.js[.inline-code] or [.inline-code]log?.txt[.inline-code].

For example:

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

This command will only extract the files with a [.inline-code].pdf[.inline-code] extension from the [.inline-code].tar.gz [.inline-code] archive.

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

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

[#unzip-multiple-tar-gz-file] Unzipping multiple tar.gz archives [#unzip-multiple-tar-gz-file]

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

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

Where:

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

Note that this loop will extract the contents of the [.inline-code].tar.gz[.inline-code] files in the current directory.

[#download-and-unzip-with-wget] Downloading and unzipping using Wget [#download-and-unzip-with-wget]

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

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

Where: 

  • [.inline-code]wget -c[.inline-code] is used to download files from the specified [.inline-code]URL[.inline-code].
  • [.inline-code]-O -[.inline-code] is used to specify that [.inline-code]wget[.inline-code] 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 [.inline-code]sudo[.inline-code].

[#unzip-a-tar-gz-file-with-python] Unzipping a tar.gz file using a Python script [#unzip-a-tar-gz-file-with-python]

To extract a [.inline-code]tar.gz[.inline-code] file with Python, you can use the [.inline-code]tarfile[.inline-code] module as follows: 

 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()

[#unzip-tar-gz-files-with-gui-apps] Unzipping tar.gz files using GUI and online alternatives [#unzip-tar-gz-files-with-gui-apps]

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