How do I gunzip to a different destination directory?

83

16

How do I gunzip to a destination directory other than the current one?

This did not work:

gunzip *.gz /putthemhere/

Scott Szretter

Posted 2010-05-09T23:15:23.850

Reputation: 1 433

Answers

110

Ask gunzip to output to standard output and redirect to a file in that directory:

gunzip -c file.gz > /THERE/file

zcat is a shortcut for gunzip -c.

If you want to gunzip multiple files iterate over all files:

for f in *.gz; do
  STEM=$(basename "${f}" .gz)
  gunzip -c "${f}" > /THERE/"${STEM}"
done

(here basename is used to get the part of the filename without the extension)

Benjamin Bannier

Posted 2010-05-09T23:15:23.850

Reputation: 13 999

3Creates the file, but does not preserve file ownership, permissions etc. That may be good or bad depending on your precise situation. – Chris Johnson – 2014-11-09T17:12:41.590

how to gunzip to different location alongwith deleting the original file? – kaushal agrawal – 2020-01-07T13:25:23.697

2

If you need to extract a single file and write into a root-owned directory, then use sudo dd:

zcat filename.conf.gz | sudo tee /etc/filename.conf >/dev/null

If the file is coming from a remote source (i.e., ssh, curl https, etc), you can do it like this:

ssh remoteserver cat filename.conf.gz | zcat | sudo tee /etc/filename.conf >/dev/null

(Note that these examples only work for a single file, unlike the example *.gz, which is all gzipped files in the directory.)

Jamieson Becker

Posted 2010-05-09T23:15:23.850

Reputation: 331

For writing with root privileges, sudo tee $filename >/dev/null is a little more idiomatic than using dd. – dcoles – 2018-11-17T21:17:20.173

1

You can try with > to redirect the result to the place you want.

jangelfdez

Posted 2010-05-09T23:15:23.850

Reputation: 202