How do I unzip a tar gz archive to a specific destination?

109

18

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.

alwbtc

Posted 2011-10-19T08:15:51.057

Reputation: 2 367

Answers

172

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

Patches

Posted 2011-10-19T08:15:51.057

Reputation: 14 078

what -C stands for? – AlikElzin-kilaka – 2015-01-28T15:56:14.420

2@AlikElzin-kilaka From man tar -C, --directory DIR change to directory DIR – tachomi – 2015-01-28T16:31:57.110

2tar -xvjf foo.tar.bz2 for those who are looking for bz2 like me – vladkras – 2016-01-26T07:09:24.353

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 – 2016-10-08T23:59:33.710

thanks, how about gzip -dc archive.tar.gz | tar -xf - -C /destination ? – alwbtc – 2011-10-19T13:12:39.873