Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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 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.

share|improve this question
up vote 7 down vote accepted

try something like

for a in *.gz; do gunzip -c $a > somedir/`echo $a | sed s/.gz//`; done
share|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

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.

share|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
    
This extracts my text files from .gz files all garbled. – Andy Apr 19 '13 at 16:52

Assuming that each file has a name like

foo.bar.gz

basename (GNU coreutils 8.4) can give you the original uncompressed file's name, thus I'd do

for f in /source/dir/*.gz; do t=$(basename $f .gz); gunzip -c $f > /target/dir/$t; done
share|improve this answer

This also works. Just another way of doing it. It will not garble up your extracted text files. I have actually tested this.

for f in *.gz; do
  STEM=$(basename "${f}" .gz)
  gunzip -c "${f}" > /somedir/"${STEM}";
done
share|improve this answer

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

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

share|improve this answer

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .