on using tar.
© 2013 Jan Zulawski <fdd@altair.pw>


To extract from a specific hierarchy level, consider that a tar(1) call has the following structure:

$ tar xvvv[{z,j}]f foo.tar[.{gz,bz,bz2}] [/desired/directory/]

where "`[]'" denotes an optional argument or string.

Called this way, tar(1) will extract ([and filter]) the file foo.tar[.{gz,bz,bz2}] starting only from the path "desired/directory/", i.e., only the contents of "desired/directory/" (recursively, that's understood).

The point is that, for tar(1) to be able to select the right level, the path must be provided _extactly_ as it is stored in the tarball. So do not assume that it has leading `/'s, or some other oddity. It is not just recommended, but sometimes mandatory that you make sure that you are specififying the correct path. Therefore, it is probably better to examine the contents of the archive first (this is good practice, anyway):

$ tar tvvvf foo.tar[.{gz,bz,bz2}]

Say we have a tarball, "hairball.tar".

$ tar tf hairball.tar

If we want only the contents of "hair/cat/", we do the following:

$ tar xvvvf hairball.tar ./hair/cat/

Note the use of the relative path as an argument, which matches the one listed by `tar tf'. Up to this, it should be pretty clear as to why tar(1) acts this way. By default, it stores relative paths.

Moreover, if you try to create a tar of a whole directory hierarchy, tar(1) will inform you on this matter:

$ tar cvvvf foo.tar /some/dir/
tar: Removing leading `/' from member names

So, if `tar tf' says the tar members have a leading `./', then place it on the extraction argument also. If there are no leading characters, then don't.

Note that tar(1) supports the `-P' option, which archives the files using their respective original absolute names (so no basename(1)-like behaviour), i.e., it doesn't strip the leading `/'s from file names.

Thus, to sum up, the call format for the GNU tar(1) looks something like this:

tar -<options> <source_tar> <path_to_directory_to_extract_from_tar>

-- Apr 08, 2013.

black celebration

[ up ]