-2

I have some files in a directory; some of them are .qcow2 extensions. I want to backup only the files having .qcow2 extension.

Example: I have 5 files with .qc0w2 exxtension, they are: vm1.qcow2, vm2.qcow2,vm3.qcow2, vm4.qcow2 and minnow.qcow2. My intention is to use the gzip command to back them up into the /backup directory. It should be like the following: vm1.qcow2.backup.gz, vm2.qcow2.backup.gz, vm3.qcow2.backup.gz, vm4.qcow2.backup.gz and minnow.qcow2.backup.gz

I tried the following command:

   gzip < *.qcow2 > /backup/*.qcow2.backup.gz 

but this is not working. Can you please help me what mistakes I have done?

  • 4
    At least show the error & result, versus what you expected. – Jeff Schaller Jun 6 '18 at 17:31
  • Example: I have 5 files with .qc0w2 exxtension, they are: vm1.qcow2, vm2.qcow2,vm3.qcow2, vm4.qcow2 and minnow.qcow2. Now My intention is to use gzip command to back them up in /backup directory. it should be like the following: vm1.qcow2.backup.gz, vm2.qcow2.backup.gz, vm3.qcow2.backup.gz, vm4.qcow2.backup.gz and minnow.qcow2.backup.gz – Masum Billah Jun 7 '18 at 13:50
1

Since you don't want a combined archive, loop over each file and compress it to its own backup file:

for file in *.qcow2
do
  gzip -c "$file" > /backup/"$file".backup.gz
done

The -c option says to send the compressed results to stdout, which we redirect to the filename we'd prefer.

  • thanks a lot. before that do i need to do this command??? $file = ls | grep *.qcow2 – Masum Billah Jun 10 '18 at 4:39
  • Absolutely not. The file variable gets assigned as part of the for loop and the *.qcow2 glob. – Jeff Schaller Jun 10 '18 at 12:12
  • but what if I want to mention the location. I mean from which directory it will search for the .qcow2 files. – Masum Billah Jun 10 '18 at 18:17
  • The current directory, just like your example in the question. – Jeff Schaller Jun 10 '18 at 18:49
  • but if i want to mention the location specific??? – Masum Billah Jun 11 '18 at 23:13
3

gzip by itself doesn't have an option to output the zip files to another directory, you would have to do that in another command.

gzip *.qcow2 -S .backup.gz
mv *.qcow2.backup.gz /backup

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.