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

1 Answer 1

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.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.