Linux Tar – How to Extract Specific Folder to Another Directory

linuxtar

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.

Best Answer

What you're looking for is the -C option:

$ man tar
<snip>

-C, --directory DIR
       change to directory DIR

Usage, per your example:

$ tar xvzf archivename.tar.gz -C /tmp/testdir/ sampledir/

Also, you may be interested to know that modern versions of tar don't require the z flag to decompress gzipped files; they'll notice it's a .gz and Do The Right Thing automatically.

Related Topic