Board index Linux Linux - Tips&Tricks

Moderator: chandranjoy

How to extract a single file or a few files from a tar file?

Postby chandranjoy » Wed Sep 01, 2010 9:12 pm

How to extract a single file or a few files from a large tarball instead of untaring the entire tarball?

The following command will extract a single file from a tarball:

# tar xf file.tar full/path/to/file


For example, the following tarball is generated:
# tar cvf pictures.tar pictures
pictures/
pictures/seattle.jpg
pictures/WG.jpg


And you want to extract seattle.jpg from the tarball without untarring the entire file set. The following command will extract it:

# tar xf pictures.tar pictures/seattle.jpg
# ls -al pictures/seattle.jpg
-rw-rw-r-- 1 user user 1458298 Jul 26 16:16 pictures/seattle.jpg


Note: You must provide the FULL path to the file you want to extract.

Somebody prefers the 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.

#Enjoy :)
chandranjoy
Site Admin
 
Posts: 283
Joined: Fri Oct 23, 2009 11:19 pm

Return to Linux - Tips&Tricks

Who is online

Users browsing this forum: No registered users and 1 guest


cron