I am new to the world of Linux and seem to have run into a stumbling block. I know I can extract a specifc archive using the command tar xvfz archivename.tar.gz sampledir/
however how can I extract sampledir/
to testdir/
rather than the path that the archive is in e.g.currently the archive is in the path /tmp/archivename.tar.gz
and I would like to extract sampledir
to testdir
which is in the path /tmp/testdir
.
What you're looking for is the
-C
option:Usage, per your example:
Also, you may be interested to know that modern versions of
tar
don't require thez
flag to decompress gzipped files; they'll notice it's a.gz
and Do The Right Thing automatically.You don't need to play crazy games with untarring everything. Tar fully supports only extracting a subset of the files in an archive, as shown in the following (trivial) example:
The trick is that the pathspec must match the contents of the tarfile exactly. So when you do a
tar tf
, if the path starts with./
, you've got to put that in, and if it doesn't have it, you have to leave it out. Also, if you want to extract a sub-sub-sub-sub-sub-sub-sub-path, it'll end up N levels deep in the filesystem when you're done.