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:
$ tar -xvzf <archive>
Run in Warp
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:
$ tar -xvzf ~/Downloads/documents.tar.gz
Run in Warp
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:
$ tar -xf <archive> -C <folder>
Run in Warp
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:
$ tar -xf archive.tar.gz -C /home/user/desktop
Run in Warp
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:
$ tar -tf <archive>
Run in Warp
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:
$ tar -xf <archive> --wildcards '<pattern>'
Run in Warp
Where:
- pattern is a shell globbing pattern like *.js or log?.txt.
For example:
$ tar -xf documents.tar.gz --wildcards '*.pdf'
Run in Warp
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:
$ tar -xf <archive> “<pattern>”
Run in Warp
Unzipping multiple tar.gz archives
To unzip multiple tar.gz archives located in the same directory at once, you can use the following command:
$ for file in *.tar.gz; do tar -xzf "$file"; done
Run in Warp
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:
$ wget -c <URL> -O - | tar -xz
Run in Warp
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:
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()
Run in Warp
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
Filed Under
Related Articles
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.
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.
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.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
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 Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
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.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.