When I run gunzip -dc /path_to_tar.gz_file/zip1.tar.gz | tar xf - in the directory where the tar.gz file is located, it extracts just fine.
How do I tell it to place the contents from the tar.gz file into a specific directory?
I tried this gunzip -dc /path_to_tar.gz_file/zip1.tar.gz | tar xf /target_directory with a tar error.
I should also note here that I am attempting to do this in a bash script and that I’m running Solaris 10.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can do a single tar command to extract the contents where you want:
tar -zxvf path_to_file -C output_directory
As explained in the tar manpages:
-C directory, –cd directory, –directory directory
In c and r mode, this changes the directory before adding the
following files. In x mode, change directories after opening the
archive but before extracting entries from the archive.
As you added that you are using Solaris, I think you could try:
gunzip -dc path_to_file | tar xf - -C path_to_extract
Method 2
Does this do what you want?
tar xzvf file.tar.gz -C /target/directory
Method 3
On modern Linux you can use:
tar xzf /path/to/file.tar.gz -C /target/directory/
This is pretty much equivalent to:
(cd /target/directory/; gzip -cd /path/to/file.tar.gz|tar xf - )
If you are not on Linux check your man pages for the supported options of your tools.
Method 4
I finally figured this out…
The find command can be used to execute any command upon each file it finds.
find . -name "*.tar" -execdir tar -vxf '{}' ; -delete
Recursively match file names (find) in the current directory (.) whose name matches regular expression (-name) anything ending with dot tar (*.tar") (double quotes prevent bash from glob expansion of *.tar — in my literal test, due to surrounding circumstances, I had to precede each quote by a backslash). Then, for every matching file, execute tar in the directory of the found file (-execdir tar) with verbosity (-v) and extract gathered files (-x) from the tarball file (-f) located by find ('{}') (single quotes prevent shell interpretation of local file name) indicating end of parameter list to be passed to tar for every matching file (;) (backslash prevents shell understanding of semi-colon) and then tell find to delete each matching file as indicated by a parameter passed to find (-delete).
Method 5
None of the other answers here mention all the caveats of the default tar implementation in Solaris. One such caveat is that it does not support compression by itself. A plain extraction can be achieved through:
gzip -dc < /path/to/the.tar.gz | tar xvf -
Further, if you created the archive by something like
tar cvf - /path/to/directory | gzip -c > the.tar.gz
you will find that extracting this archive always overwrites the original files. This is because Solaris tar does not strip leading / from archive entries upon extraction and has no means of stripping path components. So if you want to be able to extract a second copy of the contents, you will have to create the archive with a slightly different command:
tar cvf - -C /path/to/directory . | gzip -c > the.tar.gz
or
(cd /path/to/directory && tar cvf - .) | gzip -c > the.tar.gz
In the Solaris implementation, the -C switch does not apply to extraction. Assuming the archive was created using one of these two methods or similar, a variant of this second form will allow extraction to an arbitrary location:
gzip -dc < the.tar.gz | (cd /path/to/extraction/point && tar xvf -)
If you have GNU tar installed (/usr/sfw/bin/gtar), it supports compression directly, as well as path-stripping. In this case, the usual options such as
/usr/sfw/bin/gtar xvzf the.tar.gz
will suffice.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0