Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to gunzip a directory full of compressed files and pipe the output (preserving the original files) to a different directory. I need the original names to stay the same. So...

gunzip -c *.gz > file/path/filename 

# Search for all .gz in current directory and redirect output here. How do I grab the name of the file found using the wildcard?

Thank you for your help

Update: Final script below

FILES= /foo/bar/*.gz
for gz in $FILES; do
echo "unzipping $gz..." 1>&2
fn=$(basename $gz .gz)
gunzip -c $gz > /new/foobar/$fn
share|improve this question

migrated from Jun 21 '13 at 23:58

This question came from our site for professional and enthusiast programmers.

What does this have to do with Python? Also, what do you mean by "preserving the original files"? – Blender Jun 21 '13 at 18:39
Apologies; just writing python, it was on the brain. What I mean by preserving original files is that gunzip typically deletes the compressed file and outputs a file that is not compressed (hence the -c flag) – KennyC Jun 21 '13 at 18:42
up vote 1 down vote accepted

You may want to use a shell script instead:

for f in *.gz
    zcat "$f" > "other/folder/$(basename "$f" ".gz")"
share|improve this answer

With GNU Parallel you can do:

parallel gunzip -c {} '>' /new/foobar/{/.} ::: /foo/bar/*.gz

GNU Parallel takes literally 10 seconds to install:

wget -qO - | sh -x

Watch the intro videos to learn more:

share|improve this answer
Installed GNU Parallel but am getting this error; parallel: Warning: Input is read from the terminal. Only experts do this on purpose. Press CTRL-D to exit. Is it a syntax problem? Can you provide insight with the edited script in the question? – KennyC Jun 24 '13 at 16:03
Post output of 'parallel --version'. If you did not install as mentioned above, you might be hit by… – Ole Tange Jun 24 '13 at 17:00
Installed as above; no mention of tollef GNU parallel 20130622 – KennyC Jun 24 '13 at 17:40
If it works with '--gnu' then you have a '--tollef' somewhere. – Ole Tange Jun 24 '13 at 18:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.