How To Extract a Single File / Directory from Tarball Archive

Q. How do I extract a single file or directory form a tarball under UNIX / Linux shell prompt? How do I restore a single file from /dev/st0 tape device?

A. tar command allows to extract a single file or directory using the following format. It works under UNIX, Linux, and BSD operating systems.

tar xvf /dev/st0 filename
tar xvf /dev/st0 directory-name
tar xvf mytar.ball.tar filename
tar -zxvf mytar.ball.tar.gz directory-name

Extract file to /tmp directory
tar -zxvf mytar.ball.tar.gz -C /tmp filename
tar -zxvf mytar.ball.tar.gz -C /tmp dir-name

Read tar man page for more information:
man tar

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Raju Kumar Aug 25, 2009 @ 10:06


    I am running the same command to extract a directory from the tape backup. But its taking lot of time. So I have cancelled it

    Could you tell me will it take time or else there is any other command which execute faster.


  • David the Short Sep 15, 2011 @ 4:33

    tar will take time, even when extracting a single file it will take a long time to complete if the tarball is big.
    I know of nothing faster than tar to extract from a tar. Maybe just background the process and wait.

  • AdiB Sep 19, 2012 @ 18:51

    Under windows, total commander or similar could do it faster ;)

    • mrpraline Oct 12, 2012 @ 8:57

      No, there is no index in a tar archive, it is serial data. It doesn’t matter what operating system or what app you use, a tarball has to be read entirely before you know its contents.

  • Dave B Apr 16, 2013 @ 13:16

    Recovering from a tape is slower than disk. Be patient. Especially if you are retrieving a single file as mrpraline has explained it is a serial data file. If the file is at the end you will wait til the end before you even see the extraction. If the file is at the beginning, it will show its extracted and then you will wait a while before the command completes. If you want to reduce the time period of recoveries, say you are going to restore files more often, break them up into separate files. For example a tarball for /home and a tarball for /usr and /etc and / and /boot in another. That’s how I tar things up. One for the system and one for the actual user data. System is usually on my systems than data files.

  • Neeraj Verma Feb 11, 2014 @ 4:22

    You dont have to wait. Just output the file to stdout using the –to-stdout or -0 and you will see the file the instant tar gets to it on tape.

  • dhruv Apr 16, 2015 @ 9:03

    im getting this error: Exiting with failure status due to previous errors

    kindly help

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum