In the open-source world, a lot of packages come in a .tar.gz format. Consider this essentially a .zip file that we get on Windows.
To unpack these files, we use the tar command. The same command can also be used to create such archives. Originally, Tar was designed for creating archives to store files on magnetic tape, hence the name Tape Archive.
One of the most popular algorithms for compressing tar files is Gzip. By convention, the name of a file compress in tar using Gzip should be either .tar.gz or .tgz.
In this article, we’re taking a look at how you can extract or unzip .tar.gz or .tgz files.
Also read: Mint vs Ubuntu: Linux distro comparison
How to extract tar.gz files?
Pretty much every Linux and macOS distro comes with the tar command preinstalled. To extract a file, all you have to do is use the –extract or -x flag and specify the archive name after the -f flag.
tar -xf archiveName.tar.gz
The command will automatically detect the compression type and extract the archive in the active working directory. You can change the extraction directory by specifying it after the -C flag.
tar -xf archiveName.tar.gz -C /home/codery/files
You can use this command to extract tar archives compressed with other algorithms like .tar.bz2. If you add the -v flag, the command will list all the archive files before extraction.
tar -xvf archiveName.tar.gz
Also read: How to rename a file in Linux?
How to extract specific files from a .tar.gz archive?
If you’re looking for a specific file from a given archive, use the following command and mention the file name (or names) to extract exactly what you want.
tar -xf archiveName.tar.gz file1 file2 file3
Keep in mind that you have to mention the exact file name, including the path. You can print this information using the -t flag.
Extracting directories goes exactly like extracting files.
tar -xf archiveName.tar.gz dir1 dir2
Tar also allows you to extract specific files based on a wildcard character. In the below example, the command will extract all txt files present in the archive.
tar -xf archivNamee.tar.gz --wildcards '*.txt'
How to extract .tar.gz archives from stdin?
When downloading archives in the terminal, you can save yourself some time and typing by extracting the archive straight from stdin. However, you will have to specify the -z decompression flag.
wget -c https://candid.technology/source/archive.tar.gz | sudo tar -xz
Also read: How to check the list of users in Linux?
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.