How to Extract Tar Gz File?
Linux uses the
tar command to extract tar archive files. Tar is an acronym for Tape Archive that was created to archive magnetic tape files.
It is commonly used to create and extract tar.gz files. The same command can be used to create and extract files. It provides several options that can be used for several purposes like creating, moving, extracting archives, etc.
In this article, we will learn how to extract tar.gz archive files in Linux.
Command To Extract tar.gz File in Linux
tar -xf archive.tar.gz
Here, -x represent to extract and -f for file.
The tar supports a vast range of compression programs such as gzip, lzip, bzip2, lzma, xz, etc.
Extracting tar.gz File
The tar is preinstalled in all the Linux versions so you can directly use this by using the following command.
$ tar -xf archive.tar.gz
Here, -x is used to extract the archive file.
The above command will extract files in the current directory. Apart from this, if you are working on GUI based system, then you can extract files directly by right click on the file and select Extract All. It will also extract the files into the same directory.
If you want to get a detailed information of all the extract files to the terminal console then use -v option with the tar command.
$ tar -xvf archive.tar.gz
Here, -v represents verbose output to the console.
Extracting tar.gz File To Different Directory
As we know, by default files are extract in the same directory, but we can change the directory by using the -C option in the tar command. For example, if we want to move the files to a folder in the studytonight then use this command.
$ tar -xvf archive.tar.gz -C /home/studytonight/myfolder
Extracting Specific Files from a tar.gz File
Sometimes, we want to extract only selective files from the archive. In this case, specify filenames in the command.
$ tar -xvf archive.tar.gz file1 file2 file3
It will extract only the file1, file2 and file3 from the archive.
We can use the same command to extract the specific directories from the archive. While giving the file or directory name, make sure the names are correct because an incorrect name will lead to command fails.
Extracting Similar Files from a tar.gz File
If you want to extract similar type files like txt, pdf, or doc, etc from the archive. then use star(*) wildcard with the tar command and only specified files will be extracted.
$ tar -xvf archive.tar.gz --wildcards *.txt
It will extract only txt files from the archive.
Listing tar.gz file
To check the list of files in the tar archive, we can use -t option with the command.
$ tar -tvf archive.tar.gz
It will output all the files to the console without extracting the files. Since we used -v option then it will show a detailed output to the console.
The tar command can be used to compress and decompress the files. To compress files to tar.gz use -c option, and to extract files use -x option with the command. It also provides several options to work on such as moving extract files to a different directory or get a verbose output using the -v option. This detailed article covers all the aspects of the tar command.