86

I want to unpack a .tar.gz file to a specific directory.

The archive file is in /root/Documents. I want to unzip it into /root/Desktop/folder. The folder structure in zipped file should be preserved in the destination directory.

147

You have two choices:

cd /root/Desktop/folder
tar xf /root/Documents/file.tar.gz

or

tar xf file.tar.gz -C /root/Desktop/folder
  • thanks, how about gzip -dc archive.tar.gz | tar -xf - -C /destination ? – alwbtc Oct 19 '11 at 13:12
  • what -C stands for? – AlikElzin-kilaka Jan 28 '15 at 15:56
  • 2
    @AlikElzin-kilaka From man tar -C, --directory DIR change to directory DIR – tachomi Jan 28 '15 at 16:31
  • 2
    tar -xvjf foo.tar.bz2 for those who are looking for bz2 like me – vladkras Jan 26 '16 at 7:09
  • You actually don't have to specify the compression format anymore with modern tar. I edited the answer so it will work with bz2, xz, and whatever else you want to throw at it. – Patches Oct 8 '16 at 23:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.