Most Linux and open-source software files are distributed in either .tgz or .tar.gz extensions format over the Internet. These files are gzipped tarballs and include multiple files and sub-directories into a single file using the tar command. Thus, to save bandwidth, tar files are compressed using gzip program. Let us see how to extract and unpack the .tgz file on Linux.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux terminal |
Category | Archiving |
Prerequisites | tar and gunzip command |
OS compatibility | BSD • Linux • macOS • Unix • WSL |
Est. reading time | 3 minutes |
Unpacking .tgz files command
The syntax is as follows for the tar command:
$ tar zxvf fileNameHere.tgz
The GNU/tar version can automatically detect the file type and apply appropriate compression methods when unzipping those files. All you have to do is skip the -z option:
$ tar xvf fileNameHere.tgz
Or try the geeky bash pipe based syntax using the combination of gunzip command and tar command:
$ gunzip -c fileNameHere.tgz | tar xvf -
Examples
In this example, unpack a file called backups.tgz, enter:
tar zxvf backups.tgz ## OR ## tar -zxvf backups.tgz ## Extracting .tgz file on a Linux in the current directory ## tar zxvf demo.tgz
Extracting the .tgz file in different directory
Then we can use the following syntax using shell pipes too:
gunzip -c backups.tgz | tar xvf -
Sample outputs:
tar: Removing leading '/' from member names x Users/vivek/Downloads/db.txt x Users/vivek/Downloads/resume.txt x Users/vivek/Downloads/vendors.txt x Users/vivek/Downloads/sales.txt
How to unzip the .tgz file using the terminal into a specific folder (dir)
To unpack and put files in a different folder (directory) say /tmp/data, enter:
# NOTE: -C /path/to/folder/ # tar zxvf backups.tgz -C /tmp/data/ ls -l /tmp/data/
How to extract specific file(s) from .tgz
First, you can list files in .tgz using the following syntax:
$ tar -ztvf {filenme.tgz}
$ tar -ztvf backup.tgz
$ tar -ztvf backup.tgz | grep 'file1'
For multiple files try using the egrep command or pager such as less:
$ tar -ztvf backup.tgz | less
$ tar -ztvf backup.tgz | grep -E 'file1|file2'
$ tar -ztvf backup.tgz | grep -E 'rsyslog.conf|xattr.conf'
Notw down the path:
-rw-r--r-- root/root 1382 2020-02-11 20:52 etc/rsyslog.conf -rw-r--r-- root/root 642 2019-09-24 07:47 etc/xattr.conf
Next, use the tar command to extract a single file(s) from a large tarball as follows:
$ tar -zxvf backups.tgz etc/rsyslog.conf etc/xattr.conf
You can state output directory too. For example, extract to the /tmp/outputs/. Use the mkdir command to make the dir:
$ mkdir -v /tmp/outputs/
Now, type:
$ tar -zxvf backups.tgz -C /tmp/outputs/ etc/rsyslog.conf etc/xattr.conf
Verify it using the ls command:
$ ls -l /tmp/outputs/
$ ls -l /tmp/outputs/etc/
Understanding the tar command unpack .tgz file options
The options used so far as follows:
- -z : Uncompress the resulting archive with gunzip command or gunzip command.
- -x : Extract to disk from the archive.
- -v : Produce verbose output i.e. show progress and file names while extracting files.
- -t : List files stored in the archive.
- -f backup.tgz : Read the archive from the specified file called backup.tgz.
- -C /tmp/data : Unpack/extract files in /tmp/data instead of the default current directory.
- etc/rsyslog.conf or etc/xattr.conf : Extract these two files from the archive.
Getting help is easy, use the man command/info command (or pass the --help option to read documentation installed on your machine:
$ man tar
# gnu info pages #
$ info tar
$ man gzip
$ man gunzip
# The '--help' may not work on BSD/macOS & Unix.
# GNU/Linux only option #
$ tar --help
$ gunzip --help
Summing up
You learned about unpacking and extracting .tgz files on Linux and Unix-like systems. One can also use long options that are easy to recall like as follows instead of short command-line options:
## instead of short option tar zxvf /path/to/archive.tgz tar zxvf /path/to/archive.tgz -C /path/to/dest/dir1 ## get specific files only ## tar ztvf /path/to/archive.tgz | less tar zxvf /path/to/archive.tgz -C file1 file2 tar zxvf /path/to/archive.tgz -C /path/to/dest/dir1 path/file1 path/file2 ## you can use long options ## tar --gzip --extract --verbose --file=/path/to/archive.tgz tar --gzip --extract --verbose --file=/path/to/archive.tgz --directory=/path/to/dest/dir1/
🥺 Was this helpful? Please add a comment to show your appreciation or feedback.
Next FAQ: OpenBSD: Reload / Restart / Stop dhcpd Server Command
Previous FAQ: Nginx: 301 Redirect To A Domain Name
Hello.
You can try too:
It’s just what i have been looking for. Much thanks.
Thank you for the information, I was able to find what i was looking for in a jiff and i was able to understand the instructions.
Hi Folks..!
i have created a Tarball
“softwaredevelopment.war.tgz” size 1.2G
while extracting it
i used all the above command mentioned on this page
tar zxvf fileNameHere.tgz
gunzip -c fileNameHere.tgz | tar xvf –
i have also installed GNU software… but still not working for me..!
is there any solution for this… kindly help…!
tar -zxvf softwaredevelopment.war.tgz
Wanted to unzip /media/usb/db.tgz file using the terminal and this page was very much just what the doctor ordered.
Thanks! Great having a page with the solution concise and right at the front (which extra material and examples for those inclined *after* the main point).
yes it was helpful.
Thanks for this helped so much.