To setup any new software or service on your Linux machine, if you are not using any package manager, you will have to download the ZIP or TAR.GZ file for any software, then unzip it and then run any command to start that service.
A .tar.gz file is just a simple archive file that acts as a container for other files. The tar program provides the ability to create tar archives. Such a zip file can contain multiple files, folders, sub folders all packed in a single file, which is generally created using gzip or bzip2 program on Unix operating systems.
Interestingly, TAR stands for tape archiver.
The tar
command
In Unix based operating systems, we can use the tar
command to zip multiple files, folders etc into a TAR file and for unzipping a TAR file to access its files and folders.
On top of archiving a set of files and folders using the tar command, we can also compress them using the gzip
command which leads to the creation of the file with extension .tar.gz, which we want to unzip.
1. Basic usage of tar command
If your tar file is compressed using a gzip compressor, use this command to uncompress it.
$ tar xvzf somefile.tar.gz
Following are the options that we have used with the tar command:
x
: This option tells tar
to extract the files.
v
: The v
stands for verbose. This option will list all of the files one by one in the archive as they are unzipped.
z
: The z
option is very important and tells the tar
command to uncompress the file (gzip).
f
: This options tells tar
command that you are going to give it a file name to work with.
2. Unzip the tar file to specific directory
To uncompress tar.gz file into a different directory, use the command below:
$ tar xvzf somefile.tar.gz -C /path/to/directory
In the above command we have specified the -C argument to give the path of the directory in which we want the tar file to be unzipped. If we do not provide any path then the tar file is unzipped at the current location.
3. To extract a specific file out of TAR file
If we want to extract only a single file from the tar file, we can do so by specifying the name of the file that we want to extract out of the tar file, as follows:
$ tar -xvzf somefile.tar.gz test.txt
If we run the above command then the file test.txt will be extracted out of the somefile.tar.gz file and will be stored in the current location.
4. To see a table of files in the tar file
If you first wish to check what all files are zipped inside the tar file. And you wish to see the list in a tabular form, then you can use the following command:
$ tar -tvf somefile.tar.gz
Conclusion:
Hope this simple tutorial helps you in understanding how to unzip a tar file in Linux OS in various different ways. If you know some other related command that we might have missed here, do let us know in the comments.
You may also like: