Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
you can change your current dir to dir where you want to extract the tar and run the extract command from there like tar xf /path/to/t.tar.gz and then change the path back to original dir – g4ur4v Jul 18 '14 at 18:22
@g4ur4v How do I change the directory in the execution of the bash script? I thought that wasn't possible from my searching... – DemiSheep Jul 18 '14 at 18:23
when you run a shell script ,its current working dir is the one from where it is triggered , you can use cd command in your shell script and run extract and then use cd - to come back to the original dir. – g4ur4v Jul 18 '14 at 18:26
Or better use the -C switch as mentioned in the answers. – g4ur4v Jul 18 '14 at 18:27

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
share|improve this answer
I'm running Solaris 10 - when I run this I get tar: z: unknown funciton modifier – DemiSheep Jul 18 '14 at 18:31
@DemiSheep Without the - before zxvf like in the other answer maybe? Also maybe indicate the version of tar you're using... – Survenant Lazurite Jul 18 '14 at 18:55
Without the - I get the same tar: z: unknown function modifier message. I don't know how to get the version of tar I'm using. – DemiSheep Jul 18 '14 at 19:18
@illuminÉ: Solaris' standard tools are, ahem, very POSIX-compliant; it's possible that z (and j) are GNU extensions. – Ulrich Schwarz Jul 18 '14 at 19:19
@DemiSheep: do you have a gtar? If I remember, that's sometimes a name unter which GNU tar is installed on Solaris boxes. – Ulrich Schwarz Jul 18 '14 at 19:20

Does this do what you want?

tar xzvf file.tar.gz -C /target/directory
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.