Recursively unzip files and then delete original file, leaving unzipped files in place from shell
Solution 1
have you tried:
find . -depth -name '*.zip' -exec /usr/bin/unzip -n {} \; -exec rm {} \;
or
find . -depth -name '*.zip' -exec /usr/bin/unzip -n {} \; -delete
or running a second find after the unzip one
find . -depth -name '*.zip' -exec rm {} \;
Solution 2
thx for the 2nd command with -delete! helped me a lot.. just 2 (maybe helpful) remarks from my side:
-had to use '.zip' instead of `.zip` on my debian system
-use -execdir instead of -exec > this will extract each zip file within its current folder, otherwise you end up with all extracted content in the dir you invoked the find cmd.
find . -depth -name '*.zip' -execdir /usr/bin/unzip -n {} \; -delete
THX & Regards, Nord
Solution 3
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.
Solution 4
Unzip archives in subdir based on the file name (../file.zip -> ../file/..):
for F in $(find . -depth -name *.zip); do unzip "$F" -d "${F%.*}/" && rm "$F"; done
Related videos on Youtube
Ben
Updated on April 01, 2020Comments
-
Ben about 4 years
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!
-
Ben about 13 yearsI get a
find: paths must precede expression: /usr/bin/unzip
. -
Ben about 13 yearsAh! The second option works perfectly.
-delete
, how I never knew ye! One of the main criteria that of course I forgot to mention is that the unzipping and deleting should happen back to back, as the drive is not large enough to unzip everything and then go back to delete. Thank you so much! -
flolo about 13 yearsWarning: the third option does NOT work when in the zip files are other zip files.
-
cmcginty almost 9 years-1 warning this command will extract all zips into your working dir. It would be better if the zips were left in the path they were found in.
-
Luca Citi over 5 yearsNote that if you have more than one
(i)name
clause, these must be enclosed in parentheses:find \( -iname "*.zip" -o -iname "*.7z" \) -execdir 7z x {} \; -delete
-
Luca Citi over 5 yearsYou could add an additional remark that if you have more than one
(i)name
clause, these must be enclosed in parentheses:find \( -iname "*.zip" -o -iname "*.7z" \) -execdir 7z x {} \; -delete
. Also note thatiname
may be better as it also matches.ZIP
.