Tar Extract a Single File(s) From a Large Tarball

by Vivek Gite · 9 comments

Q. I've couple of large tarballs such as www.tar and images.tar. Is it possible to extract a single file or a list of files from a large tarball such as images.tar instead of extracting the entire tarball? How do I extract specific files under Linux / UNIX operating systems?

A. GNU tar can be used to extract a single or more files from a tarball. To extract specific archive members, give their exact member names as arguments, as printed by -t option.

Extracting Specific Files

Extract a file called etc/default/sysstat from config.tar.gz tarball:
$ tar -ztvf config.tar.gz
$ tar -zxvf config.tar.gz etc/default/sysstat
$ tar -xvf {tarball.tar} {path/to/file}

Some people prefers following syntax:
tar --extract --file={tarball.tar} {file}
Extract a directory called css from cbz.tar:
$ tar --extract --file=cbz.tar css

Wildcard based extracting

You can also extract those files that match a specific globbing pattern (wildcards). For example, to extract from cbz.tar all files that begin with pic, no matter their directory prefix, you could type:
$ tar -xf cbz.tar --wildcards --no-anchored 'pic*'
To extract all php files, enter:
$ tar -xf cbz.tar --wildcards --no-anchored '*.php'

Where,

  • -x: instructs tar to extract files.
  • -f: specifies filename / tarball name.
  • -v: Verbose (show progress while extracting files).
  • -j : filter archive through bzip2, use to decompress .bz2 files.
  • -z: filter archive through gzip, use to decompress .gz files.
  • --wildcards: instructs tar to treat command line arguments as globbing patterns.
  • --no-anchored: informs it that the patterns apply to member names after any / delimiter.

Further readings:

  • GNU tar man page

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 9 comments… read them below or add one }

1 sathiya 12.06.08 at 11:42 am

excellent article..

2 ruben 12.08.08 at 8:13 am

I suggest getting coffee especially, Extracting files from large tar files.
Instead of using tar, it’s far better to use zip as methode. the reason for this is that zip knows where the files are.

We have some 4 Gb tar files and extracting a couple of single files with tar can take up to 25 minutes.

My 2 cents,

Ruben

3 elinus liga 12.15.08 at 11:09 am

Excellent post. It’s really useful. Clear, simple and just as what we need.

4 zaffar 12.22.08 at 12:12 pm

excellent article for new bie

5 kpb 02.24.09 at 1:33 am

Great article!
Keep it coming!

6 Jatin 04.22.09 at 11:40 am

Great help !!! thanks ..

7 subba 05.15.09 at 4:43 am

Good one! Thanks a lot!

8 Asad Kahwaja 06.23.09 at 7:08 pm

My problem is that i have downloaded a movie.
But my mvie is corrupt in da end
now i have downloaded 8th part of the movie.
but when i extract it
i gives me error
” YOU MUST HAVE THE PREVIOUS FILES TO EXTRACT “…
is dere any way i can resole this n can only extract on file.

9 Kevin H 07.28.09 at 8:48 pm

is there a way to use regexp matching rather than wildcard matching with tar?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All