Home > Enterprise >  How to gunzip all the files with a given extension and send to another folder?
How to gunzip all the files with a given extension and send to another folder?

Time:10-12

I have a folder with 28 gz files with the extension .gz and 28 files with the extension .gz.bam.

I would like to unzip all the 28 .gz files and send them to another folder. I was doing one by one as follows:

gunzip -c file1.gz > /mnt/s3/data_transfer/file1

How can I specify I want the .gz and not the .gz.bam?

CodePudding user response:

This might be what you're looking for:

for f in *.gz; do gunzip -c "$f" > /mnt/s3/data_transfer/"${f%.gz}"; done

You may remove the .gz files by rm *.gz after that, if you want. Or, alternatively

cp *.gz /mnt/s3/data_transfer/ && cd /mnt/s3/data_transfer && gunzip *.gz

Note that the latter command will gunzip all .gz files in the directory /mnt/s3/data_transfer, including the ones that exist, if any, before the cp command is executed. If you want to remove the original .gz files, replace cp with mv.

CodePudding user response:

First, let's find the files:

find . -type f -name "*.gz" > foo.sh

We find the files whose name ends with gz and save them into foo.sh. Let's open it:

vim foo.sh

Now, we prepend the gunzip command to each line. Let's hit the : key, so at the bottom of the file you can enter a command. Now, let's enter this command:

%s/^/gunzip -c /

this replaces the start of each line with the text we want to prepend.

Then we append the destination path, by pressing : again and pasting the following:

%s/$/ \/some\/path/

of course, instead of \/some\/path you will need to use your own path.

Finally, add

cd /some/path
rename 's/\.gz//' *

to remove all the gz extensions from the files in /some/path (replace this with your actual path)

Note that you may need to install rename and here I assumed that the target path already existed.

  • Related