Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively unzip files and then delete original file, leaving unzipped files in place from shell

Tags:

bash

unzip

rm

I've so far figured out how to use find to recursively unzip all the files:

find . -depth -name `*.zip` -exec /usr/bin/unzip -n {} \; 

But, I can't figure out how to remove the zip files one at a time after the extraction. Adding rm *.zip in an -a -exec ends up deleting most of the zip files in each directory before they are extracted. Piping through a script containing the rm command (with -i enabled for testing) causes find to not find any *.zips (or at least that's what it complains). There is, of course, whitespace in many of the filenames but at this point syntaxing in a sed command to add _'s is a bit beyond me. Thank for your help!

like image 270
Ben Avatar asked Apr 08 '11 04:04

Ben


1 Answers

As mentioned above, this should work.

find . -depth -name '*.zip' -execdir unzip -n {} \; -delete

However, note two things:

  • The -n option instructs unzip to not overwrite existing files. You may not know if the zip files differ from the similarly named target files. Even so, the -delete will remove the zip file.
  • If unzip can't unzip the file--say because of an error--it might still delete it. The command will certainly remove it if -exec rm {} \; is used in place of -delete.

A safer solution might be to move the files following the unzip to a separate directory that you can trash when you're sure you have extracted all the files successfully.

like image 132
John D. Avatar answered Oct 17 '22 07:10

John D.