Tar – How to Use Gunzip and Tar to Extract tar.gz File to Specific Directory

gziptar

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.

Best Answer

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
Related Question