This post will guide you how extract tar.gz or .tgz files using the TAR command under Linux command line. How do I decompress tar.gz file under CentOS or RHEL or Ubuntu Linux operating system from the command line.
The Tar file format is common in Linux or Unix system, but only can be used to store data, and not compressing it. And Tar Files are also can be compressed after being created. And those files become tgz files, using the tgz, tar.gz, or gz extension.
- Tar Command Options
- Extracting tgz Files
- Extract tar.gz Files
- Extracting tar.bz2 files
- Extracting tgz/tar.gz/tar.bz2 Files to a Different Directory
- Extracting a Single File from a Tar File
- List the Content of a Tar File
Tar command options
-t: List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.
-v: Verbosely list files processed.
-z: Filter the archive through gzip
-j, –bzip2: Filter the archive through bzip2(1).
-J, –xz: Filter the archive through xz(1).
Extracting tgz Files
If you want to extract a tgz file from your Linux command line, you can issue the following tar command, type:
$ tar -zxvf mytest.tgz
Outputs:
devops@devops:~/tmp$ tar -zxvf mytest.tgz
test1
test2
test3
devops@devops:~/tmp$ ls test*
test1 test2 test3
Extract tar.gz Files
If you have a tar file that is compressed using a gzip compressor, and you can extract all files from this tar.gz file by issuing the following tar command to decompress it, type:
$ tar -zxvf mytest.tar.gz
Outputs:
devops@devops:~/tmp$ tar -zxvf mytest.tar.gz
wildfly-17.0.1.Final/
wildfly-17.0.1.Final/bin/
wildfly-17.0.1.Final/bin/client/
wildfly-17.0.1.Final/standalone/
wildfly-17.0.1.Final/standalone/configuration/
wildfly-17.0.1.Final/standalone/lib/
wildfly-17.0.1.Final/standalone/lib/ext/
wildfly-17.0.1.Final/standalone/tmp/
wildfly-17.0.1.Final/standalone/tmp/auth/
wildfly-17.0.1.Final/standalone/deployments/
wildfly-17.0.1.Final/.well-known/
wildfly-17.0.1.Final/.well-known/acme-challenge/
wildfly-17.0.1.Final/appclient/
wildfly-17.0.1.Final/appclient/configuration/
wildfly-17.0.1.Final/docs/
wildfly-17.0.1.Final/docs/licenses/
wildfly-17.0.1.Final/docs/contrib/
wildfly-17.0.1.Final/docs/contrib/scripts/
wildfly-17.0.1.Final/docs/contrib/scripts/init.d/
wildfly-17.0.1.Final/docs/contrib/scripts/systemd/
wildfly-17.0.1.Final/docs/contrib/scripts/service/
wildfly-17.0.1.Final/docs/contrib/scripts/service/amd64/
wildfly-17.0.1.Final/docs/schema/
wildfly-17.0.1.Final/docs/examples/
wildfly-17.0.1.Final/docs/examples/configs/
wildfly-17.0.1.Final/.installation/
wildfly-17.0.1.Final/welcome-content/
wildfly-17.0.1.Final/modules/
wildfly-17.0.1.Final/modules/system/
wildfly-17.0.1.Final/modules/system/layers/
……
Extracting tar.bz2 files
If you have a tar.bz2 file that is compressed using bZip2 compressor, and you want to extract all files from it. And you can issue the following tar command with the j option. Type:
$ tar -jxvf mytest.tar.bz2
Outputs:
devops@devops:~/tmp$ tar -jxvf mytest.tar.bz2
test1
test2
test3
devops@devops:~/tmp$ ls test*
test1 test2 test3
Extracting tgz/tar.gz/tar.bz2 Files to a Different Directory
By default, the tar command will extract all files into the current directory. And if you want to extract the content of tar/tar.gz/tar.bz2 files into a different directory, and you need to specify a directory in tar command.
For example, you want to extract archive files into another directory called /tmp, you can use the following command:
$ tar -zxvf mytest.tgz -C /tmp $ tar -zxvf mytest.tar.gz /tmp $ tar -jxvf mytest.tar.bz2 /tmp
Extracting a Single File from a Tar File
If you want to extract a single file from a given tgz/tar.gz/tar.bz2 file in your Linux system, you just need to pass the file name pattern of your single file that you want to extract.
For example, you want only to extract a file called test1. Just issuing the following commands:
$ tar -zxvf mytest.tgz test1 $ tar -zxvf mytest.tar.gz test1 $ tar -jxvf mytest.tar.bz2 test1
List the Content of a Tar File
If you only want to view all files from a tgz/tar.gz/tar.bz2 file in Linux, and you can use the tar command with the -t option. Just run the following command from the command line:
$ tar -ztvf mytest.tgz $ tar -ztvf mytest.tar.gz $ tar -jtvf mytest.tar.bz2 $ tar -Jtvf mytest.tar.xz
Conclusion
You should know that how to extract or decompress a tgz/tar.gz/tar.bz2 file using the tar command in Linux.