How to extract gzip tarball archive *.tar.gz to a specific destination directory

There are to operations required in order to extract the content from gziped tarball archive file. Consider a following example where we first create a small gzip tarball archive named gzip-tarball.tar.gz:

mkdir archive
$ touch archive/linuxconfig
$ tar cvzf gzip-tarball.tar.gz archive/
archive/
archive/linuxconfig
$ ls -l gzip-tarball.tar.gz
-rw-rw-r--. 1 lrendek lrendek 155 May 26 09:22 gzip-tarball.tar.gz

Although we have used a single command tar to compress our archive/ directory the fact is that with a tar‘s option z we have also called gzip command to help with a compression.

First, the tar command created a tarball from our archive directory and then the gzip command compressed a content of gzip-tarball.tar to gzip-tarball.tar.gz. As a result, extract command tar xzf will first decompress a gzip tarball with use of gzip and then extract tarball content from stream with the tar command.

Consequently, to extract gzip compressed tarball to a specific directory we need to use two tools tar and gzip or gunzip. Simple solution could be to use a pipe to redirect output from gunzip command to tar. Let’s say we would like to decompress our gzip-tarball.tar archive to a directory /tmp/dir1

$ gunzip -c gzip-tarball.tar.gz | tar x -C /tmp/dir1/

The above gzip archive is not decompressed into directory /tmp/dir1/:

$ ls /tmp/dir1/archive/
linuxconfig