Linux: tar Extract Files

How can I extract or uncompress a file from tar ball downloaded from the Internet under Linux using bash command prompt?

You need to use the tar command to extract files from an archive or to create an archive (also known as tarball). A tarball or an archive is nothing but a single file that contains various individual files. It also includes information which allows your to restore files to their original form by one or more extraction programs such as tar command.

ADVERTISEMENTS

Extract or Unpack a TarBall File

To unpack or extract a tar file, type:

tar -xvf file.tar

To save disk space and bandwidth over the network all files are saved using compression program such as gzip or bzip2. To extract / unpack a .tar.gz (gzip) file, enter (note -z option):

tar -xzvf file.tar.gz

To extract / unpack a .tar.bz2 (bzip2) file, enter (note -j option):

tar -xjvf file.tar.bz2

Where,

  • -x : Extract a tar ball.
  • -v : Verbose output or show progress while extracting files.
  • -f : Specify an archive or a tarball filename.
  • -j : Decompress and extract the contents of the compressed archive created by bzip2 program (tar.bz2 extension).
  • -z : Decompress and extract the contents of the compressed archive created by gzip program (tar.gz extension).

How Do I Extract A Single File Called foo.txt?

To extract a single file called foo.txt, enter:

tar -xvf file.tar foo.txt
tar -xzvf file.tar.gz foo.txt
tar -xjvf file.tar.bz2 foo.txt

You can also specify path such as etc/resolv.conf, enter:

tar -xvf file.tar etc/resolv.conf
tar -xzvf file.tar.gz etc/resolv.conf
tar -xjvf file.tar.bz2 etc/resolv.conf

How Do I Extract a Single Directory Called etc?

To extract a single directory called etc, enter:

tar -xvf file.tar etc
tar -xzvf file.tar.gz etc
tar -xjvf file.tar.bz2 etc

Sample outputs:

etc/
etc/pulse/
etc/pulse/default.pa
etc/pulse/client.conf
etc/pulse/daemon.conf
etc/pulse/system.pa
etc/xml/
etc/xml/docbook-xml.xml.old
etc/xml/xml-core.xml
etc/xml/catalog
etc/xml/catalog.old
etc/xml/docbook-xml.xml
etc/xml/rarian-compat.xml
etc/xml/sgml-data.xml
etc/xml/xml-core.xml.old
etc/xml/sgml-data.xml.old
etc/mail.rc
etc/Wireless/
etc/Wireless/RT2870STA/
etc/Wireless/RT2870STA/RT2870STA.dat
etc/logrotate.conf
etc/compizconfig/
etc/compizconfig/config
.....
...
....

etc/python/
etc/python/debian_config
etc/ConsoleKit/
etc/ConsoleKit/seats.d/
etc/ConsoleKit/seats.d/00-primary.seat
etc/ConsoleKit/run-session.d/
etc/ConsoleKit/run-seat.d/
etc/opt/
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
13 comments… add one
  • Scooter Mar 26, 2013 @ 5:45

    Great guide, thanks!

  • Reedyseth Apr 3, 2013 @ 23:25

    So far this is the best guide that I have found to unpack Tars files, thanks for posting it.

  • Nelson Moisés Paulo Apr 4, 2013 @ 10:59

    The commands worked fine. Thanks a lot for the explanation.

    Muduv@
    Maputo-Mozambique

  • Steve May 9, 2013 @ 15:41

    Thanks for the info.. I dont know how many times I’ve come here to find this one simple command, but I dont seem to be able to memorize it!! This time, I THOUGHT I had it, and did:
    tar -xfv file.tar
    So – I had it right, except for the order of the options.. Shouldn’t this have worked? It didn’t, and I’m not clear why..

    • Chris Jan 29, 2016 @ 0:58

      The -f option has to come last, to tell tar what the filename is.

  • Vitaly May 17, 2013 @ 21:44

    Universal version for bash https://github.com/xvoland/Extract

  • antarus May 28, 2014 @ 10:06

    Is there any issue if I extract a .tgz file using the command
    tar -xvf file_name.tgz

    It worked for me in my machine. Will there be any cases where it wont work?

  • MegaMauricio Jul 25, 2014 @ 15:06

    Thanks you! Very useful.

  • Cityzen Jun 8, 2015 @ 22:14

    Now, when i search for a tips on Linux, i search on google, and clic on http://www.cyberciti.biz ‘s result !
    Well explained for beginners like me, and sooo clear ! Thanks a lot.

  • Vikas Aug 4, 2015 @ 18:22

    Clear, concise, to the point. Thanks a bunch!

  • Joerg Oct 25, 2015 @ 4:30

    Question:

    How to extract a wordpress.tar.gz into the current folder without creating a new folder “wordpress”?

    thx Joerg

    • 🐧 Vivek Gite Oct 25, 2015 @ 8:28

      Try:

      tar -zxvf wordpress.tar.gz -C .
      • froilan Feb 29, 2016 @ 10:53

        its not working in my printer

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.