Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uncompress a tar.gz in another directory

Tags:

unix

gzip

tar

I have an archive

*.tar.gz

How can I uncompress this in a destination directory?

like image 594
Mercer Avatar asked Aug 23 '13 12:08

Mercer


People also ask

How do I untar a tar in a current directory in Linux?

Extract or Unpack a TarBall File-f : Specify an archive or a tarball filename. -j : Decompress and extract the contents of the compressed archive created by bzip2 program (tar. bz2 extension). -z : Decompress and extract the contents of the compressed archive created by gzip program (tar.


3 Answers

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
like image 60
Alfe Avatar answered Oct 07 '22 20:10

Alfe


gzip -dc archive.tar.gz | tar -xf - -C /destination

or, with GNU tar

tar xzf archive.tar.gz -C /destination
like image 27
Mercer Avatar answered Oct 07 '22 22:10

Mercer


Extracts myArchive.tar to /destinationDirectory

Commands:

cd /destinationDirectory
pax -rv -f myArchive.tar -s ',^/,,'
like image 38
javaPlease42 Avatar answered Oct 07 '22 20:10

javaPlease42