296

I have an archive

*.tar.gz

How can I uncompress this in a destination directory?

tchrist
  • 78,834
  • 30
  • 123
  • 180
Mercer
  • 9,736
  • 30
  • 105
  • 170
  • 9
    Why all the downvotes? There are more options than just -C (and maybe his version does not provide this). – Alfe Aug 23 '13 at 12:11
  • 2
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Aug 23 '18 at 10:41
  • 1
    @jww While I see your point we also have tags named `tar` and `gz` around in _this_ forum. Extracting a tarball into a specific directory can well be part of a shell script and thus part of programming. I guess this kind of question can be asked in either forum then. – Alfe Feb 03 '19 at 15:05

5 Answers5

354

You can use the option -C (or --directory if you prefer long options) to give the target directory of your choice in case you are using the Gnu version of tar. The directory should exist:

mkdir foo
tar -xzf bar.tar.gz -C foo

If you are not using a tar capable of extracting to a specific directory, you can simply cd into your target directory prior to calling tar; then you will have to give a complete path to your archive, of course. You can do this in a scoping subshell to avoid influencing the surrounding script:

mkdir foo
(cd foo; tar -xzf ../bar.tar.gz)  # instead of ../ you can use an absolute path as well

Or, if neither an absolute path nor a relative path to the archive file is suitable, you also can use this to name the archive outside of the scoping subshell:

TARGET_PATH=a/very/complex/path/which/might/even/be/absolute
mkdir -p "$TARGET_PATH"
(cd "$TARGET_PATH"; tar -xzf -) < bar.tar.gz
Alfe
  • 56,346
  • 20
  • 107
  • 159
117
gzip -dc archive.tar.gz | tar -xf - -C /destination

or, with GNU tar

tar xzf archive.tar.gz -C /destination
Mercer
  • 9,736
  • 30
  • 105
  • 170
  • 3
    I like the notion of using `gzip` in case the tar does not understand zipped archives. – Alfe Aug 23 '13 at 12:21
4

Extracts myArchive.tar to /destinationDirectory

Commands:

cd /destinationDirectory
pax -rv -f myArchive.tar -s ',^/,,'
javaPlease42
  • 4,699
  • 7
  • 36
  • 65
1

I'm unsure if this is a RedHat specific issue, but I had to move the -C to the front of the untar command so that the change directory executed ahead of the untar. Every other example I saw has it at the end, and for me would mean the archive was unpacked into the working dir. Example below:

tar -C /u00/app/oracle/product -xvf /var/tmp/oem-backup.tar.gz u00/app/oracle/product/OEM --strip-components=4
Iain Hunter
  • 4,319
  • 1
  • 27
  • 13
0

You can use for loop to untar multiple .tar.gz files to another folder. The following code will take /destination/folder/path as an argument to the script and untar all .tar.gz files present at the current location in /destination/folder/path.

if [ $# -ne 1 ];
 then
 echo "invalid argument/s"
 echo "Usage: ./script-file-name.sh /target/directory"
 exit 0
fi
for file in *.tar.gz
do
    tar -zxvf "$file" --directory $1
done
Rahul
  • 111
  • 1
  • 3