3

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
7

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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