Can I extract a specific folder using tar to another folder?

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.

Leave a Comment