How do I gunzip to a destination directory other than the current one?
This did not work:
gunzip *.gz /putthemhere/
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)
If you need to extract a single file and write into a root-owned directory, then use sudo tee
:
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.)
sudo tee $filename >/dev/null
is a little more idiomatic than using dd
.
sudo tee
instead of sudo dd
. The answerer replaced two instances, but it seems they just forgot to edit that part in the aforementioned suggested edit.
Commented
Jun 26, 2022 at 4:28