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.

  • 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
    Commented Jul 18, 2014 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
    Commented Jul 18, 2014 at 18:23
  • 1
    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
    Commented Jul 18, 2014 at 18:26
  • Or better use the -C switch as mentioned in the answers.
    – g4ur4v
    Commented Jul 18, 2014 at 18:27

5 Answers 5


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
  • I'm running Solaris 10 - when I run this I get tar: z: unknown funciton modifier
    – DemiSheep
    Commented Jul 18, 2014 at 18:31
  • @DemiSheep Without the - before zxvf like in the other answer maybe? Also maybe indicate the version of tar you're using...
    – user44370
    Commented Jul 18, 2014 at 18:55
  • 1
    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
    Commented Jul 18, 2014 at 19:18
  • 2
    @illuminÉ: Solaris' standard tools are, ahem, very POSIX-compliant; it's possible that z (and j) are GNU extensions. Commented Jul 18, 2014 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. Commented Jul 18, 2014 at 19:20

Does this do what you want?

tar xzvf file.tar.gz -C /target/directory

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.


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).


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


(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.

  • 1
    If you do get handed a tar archive with absolute pathnames, pax -r -s,^/,, is one way to remove the leading /. Commented May 8, 2017 at 2:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .