I know this can be done for single files, e.g.

gunzip -c my.gz > somedir/my

Can it be done for multiple files?

[UPDATE] I have a directory with a large number of .gz files (not .tar.gz), and I want to gunzip them into another directory while leaving the original files untouched.

link|improve this question
feedback

4 Answers

up vote 6 down vote accepted

try something like

for a in *.gz; do gunzip -c $a > somedir/`echo $a | sed s/.gz//`; done
link|improve this answer
Deleted my post.. Your one-liner is more simple :) – Andrejs Cainikovs Sep 29 '10 at 20:25
basename might be a little simpler: for a in *.gz; do zcat $a > somedir/basename $a; done – Neil Sep 29 '10 at 20:30
Thanks Neil, but basename $a doesn't work for me in bash on Mac as it appears above - perhaps some punctuation is missing? – Bio_X2Y Sep 29 '10 at 20:49
Thanks Andrejs, Nifle, Neil. This command does the trick. – Bio_X2Y Sep 29 '10 at 21:08
basename $a needed telling to remove the .gz. therefore: basename $a .gz – Andy Lee Robinson Jul 30 '11 at 11:54
feedback

This will work in bash

for FILE in *.gz
do
    echo -n "File $FILE... "
    gzip -c $FILE > ${FILE%.gz}
    echo "Done"
done

Building on Andreja's answer, adding correction for file names.

link|improve this answer
Forgot to add semicolons ;) – Andrejs Cainikovs Sep 29 '10 at 20:28
Sorry, where exactly do they go? :) – Bio_X2Y Sep 29 '10 at 20:35
Uhmm.. after each command within a loop? :) echo; gzip; echo; done... – Andrejs Cainikovs Sep 29 '10 at 20:50
Ah yes, I missed the one before the first "do"! This works for me if I update to use gunzip and insert the target directory. More work needed to strip the ".gz" from the target uncompressed files. Thanks. – Bio_X2Y Sep 29 '10 at 20:57
feedback

Sounds like job for a quick perl/python/ruby/etc. script.

Just adjust the code in this example to apply the necessary gunzip command.

link|improve this answer
feedback

This version preserves the original filetimes as inplace gzip does, and is "space-safe":

IFS=$'\n';for a in *.gz; do d=`basename $a .gz`;echo gunzip -c $a > somedir/$d; touch somedir/$d -r $a; done;

I found quoting "$a" and "$d" thus, was unnecessary as bash appeared to automatically escape spaces.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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