In this guide, we will show you how to unzip Tar.gz file using the Tar command.
Tar is a computer software utility available for the major operating systems, including Linux, Windows, and macOS. It is used to create a collection of many files into one archive file. Tar supports various compression methods such as gzip, compress, bzip2, xz, etc., to compress the file size.
If you come from Linux or started using Linux, definitely, you’ll encounter everywhere Tar.gz or Zip files. Because of most of the packages available to download in either tar.gz or zip format.
A file that ends with tar.gz is a tar file that has been compressed using a gzip method. Gzip is the most commonly used program to compress the file size.
Unzip Tar.gz File on Linux
Tar comes pre-installed on most of the Linux distro, so you don’t need to install Tar again.
To unzip the tar.gz archive file on the Linux, use the following command in terminal.
$ tar -xf archive.tar.gz
Replace the “archive” with the file name that you want to extract. -x is a mode specifier that tells the Tar software to extract the archive file, and -f <Filename> specifies the file path.
To extract the archive use -x and to create the archive use -c mode specifier, and then type the path of the file after the -f <filename>.
This command will extract the archive file without displaying the list of the files being extracted. To view the name of the files, use the -v option after the mode specifier.
$ tar -xvf archive.tar.gz
This command will display all the file names that are being extracted.
By default, tar will extract the archive file in the current directory. Use the argument -C to specify the directory location where you want to extract the archive.
$ tar -xf archive.tar.gz -C /var/www/html
Extract Specific Files or Folders from a Tar.gz File
To extract the specific files from the tar.gz file, append the list of one or more files name separated by space after the archive name.
$ tar -xf archive.tar.gz filename1 filename2
Similarly, to extract the one or more directory or folder from the archive file, use the following command.
$ tar -xf archive.tar.gz directory1 directory2
You must provide the exact name of the file; otherwise, you get an error message “file not found.” To list all files’ names of the archive file, including the path, use the following command.
$ tar -tf archive.tar.gz
This command will print all the files’ names, including the path, in the terminal. You can copy the path and then append the path after the archive.tar.gz.
Extract Specific Type of Files
If you want to extract the specific types of files such as Text file, js file, HTML file, etc., then you can use the wildcards (*) and then specify the file extension.
For example, to extract the file having a extension .text, use the following command.
$ tar -xf archive.tar.gz *txt
As soon as you run this command, this command will look at all the files having .txt file type, then extract them only.
List files and folders of the Tar.gz file
To list all the files and folders of the archive files, use the following command in the terminal.
$ tar -tf archive.tar.gz
This command will list all the content of the archive file.
To list only specific files, such as the text files, use the wildcards (*) before the file extension.
$ tar -tf archive.tar.gz *txt
This command will list only the files having a .txt extension.
To get more information about the file, such as permission, owner, etc., use the verbose (-v) option after specifying the mode specifier.
$ tar -tvf archive.tar.gz *txt
The output will be like the following one.
-rw-r--r-- user/user 0 2020-11-15 03:20 file1 -rw-r--r-- user/user 0 2020-11-15 03:25 file2 -rw-r--r-- user/user 0 2020-11-15 03:26 file3 drwxr-xr-x user/user 0 2020-11-15 03:25 dir1/
Tar is used to create an archive of the files; tar.gz is a tar archive compressed using a Gzip method. To unzip the tar.gz file, we use the command tar -xf archive.tar.gz.
If you face any problem using tar, please comment below.