30

What is the gzip equivalent of the following command:

tar xvzf /usr/local/file/file/file.tar.gz -C /usr/local/extract_here

I am trying

gzip -d /usr/local/file/file/file.tar.gz -C /usr/local/extract_here

but it does not work, how do I do that with gzip?

BenjiWiebe
  • 2,116
  • 5
  • 22
  • 41
gr68
  • 420
  • 1
  • 10
  • 25

3 Answers3

55

Given the fact that you have a .tar.gz file, the first way you tried, with the -C option, will work just fine:

tar xvzf /dir/to/file.tar.gz -C /dir/to/output/

tar calls gzip to decompress, and then extracts the files from the tar stream. gzip can only decompress, so gunzip file.tar.gz would simply leave with the decompressed file.tar, on which you would then need to tar xvf file.tar. The z option of tar is simply a shortcut to do the decompression with gzip along with extracting the files.

If you want to just decompress a .gz file (as opposed to extracting a .tar.gz file) to a different directory, simply pipe there. E.g. gzip -dc < file.gz > /somewhere/file.

KevinL
  • 1,238
  • 11
  • 28
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • If you want to move the question to a different sx platform I believe there is a procedure for this – mit Oct 01 '14 at 10:20
8
cd /mydir_with_gz_files
for i in *gz
do
gzip -dkc < $i > /my_destination_dir/${i%%.gz}
done

This syntax is for extracting multiple *.gz files contained in /mydir_with_gz_files into a different directory.

1) Used -k option here because otherwise gzip will NOT preserve the archive itself. Good to remember!

2) I also want to emphasize what seems obvious to those giving the correct answer but wasn't so to me and the question's author: gzip will NOT extract files into a different DIRECTORY, but will expect that the resulting uncompressed file's NAME be specified as well.

This is achieved here by the $i > ${i%%.gz} bash parameter substitution that strips a name like "file.gz" of its ".gz" suffix. With other shells the syntax will be different (if at all possible).

Kostya Berger
  • 81
  • 1
  • 2
7

In order to preserve the .tar file without GZIP compression, I use a line like this with good results:

cat  file_to_uncompress.tar.gz | gzip -d > /destination_directory/packet_uncompressed.tar

That’s an useful and intuitive form to manage packets in some situations.

dakab
  • 5,379
  • 9
  • 43
  • 67
Pablo Luna
  • 369
  • 3
  • 5