How to unpack .tgz file on a Linux - nixCraft

How to unpack .tgz file on a Linux

See all GNU/Linux related FAQ
I am a new Linux user. I am having problem with the Terminal. How do I unpack .tgz (tar.gz) file on a Linux using command line option? What command I can type to unzip .tgz file using the Linux terminal?

Most Linux and open-source software files are distributed in either .tgz or .tar.gz extensions format over the Internet.
See all UNIX related articles/faq
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 -

The commands mentioned here are compatible with Unix-like systems such as FreeBSD, OpenBSD, NetBSD, macOS and Linux distros.

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
How to extract and unpack .tgz (tar.gz) file on a Linux

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/

How to untar .tgz file and extract the specific single file(s)

How to untar .tgz file and extract the specific single file(s) {click to enlarge}

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.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

9 comments… add one
  • Alsalgo Nov 20, 2013 @ 7:29

    Hello.
    You can try too:

    gzip -dc backups.tgz | tar xvf -
    gzip -dc backups.tar.gz | tar xvf -
    gzip -dc backups.tar.Z | tar xvf -
  • paul muhia Jan 17, 2015 @ 9:43

    It’s just what i have been looking for. Much thanks.

  • adoare Jan 22, 2015 @ 18:22

    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.

  • Saud Sep 21, 2016 @ 2:35

    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…!

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Sep 21, 2016 @ 3:28

      tar -zxvf softwaredevelopment.war.tgz

  • Chia-wei Feb 2, 2022 @ 18:05

    Wanted to unzip /media/usb/db.tgz file using the terminal and this page was very much just what the doctor ordered.

  • telestew Feb 26, 2022 @ 19:14

    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).

  • ARnAV GUPTA Sep 3, 2022 @ 7:38

    yes it was helpful.

  • JoJoe Apr 7, 2023 @ 1:16

    Thanks for this helped so much.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.