Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to know whether it is possible to gunzip multiple files and rename them with one command/script.

I have a bunch of files in the format:

test.20120708191601.DAT.3599502593.gz
test.20120708201601.DAT.99932140.gz
test.20120708204600.DAT.1184686967.gz
test.20120708212100.DAT.824089664.gz
test.20120708215100.DAT.1286044098.gz
test.20120708222100.DAT.1414234861.gz

I need to gunzip them and remove everything after the .DAT, to be in the format:

test.20120708191601.DAT
test.20120708201601.DAT
test.20120708204600.DAT
test.20120708212100.DAT
test.20120708215100.DAT
test.20120708222100.DAT

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Try this:

for file in *.gz; do
  gunzip -c "$file" > "${file/.DAT*/.DAT}"
done

The approach uses gunzip's option to output the uncompressed stream to standard output (-c), so we can redirect it to another file, without a second renaming call. The renaming is done on the filename variable itself, using bash substitution (match any globbing pattern .DAT* and replace it with .DAT). The loop itself just iterates over files in the current directory with names ending with .gz.

share|improve this answer
    
Could you please explain what the loop is doing? –  Pieter van Niekerk Jul 9 '12 at 14:29
    
I hope it is clear now. –  lynxlynxlynx Jul 9 '12 at 14:47
add comment

Your Answer

 
discard

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.