1

How can I do a dynamic search of zip files in a particular path (e.g.:/opt/uploading/"*"/multiple .zip files) and unzip it into the same folder with the zip files?

Below function is unzipping multiple zip files and removing zip files. But I want zip files to be there with the unzip files.

while true; do
   find -iname '*.zip' > zipindex
   test -s zipindex || break
   for zip in $(cat zipindex); do unzip -o $zip && rm $zip; done
done
airhuff
  • 719
keerthi
  • 11
  • 2
  • So, to clarify, you'd like to extract all zip archives in the directory tree, and any zip files inside them, but avoid extracting the same file again? Doesn't that loop extract the archives in the current directory, instead of the location of the archive? Is that what you wanted? – ilkkachu Apr 18 '17 at 08:34
  • Yes, I expect to extract all zip archives in the directory tree and any zip files inside them, but avoid extracting the same file again. but I need those unzipped files in a folder and zipped files in a folder so that we can avoid duplicates or else let me know how to handle the duplicates with these code. – keerthi Apr 18 '17 at 09:29

3 Answers3

1

Well the rm $zip is removing the .Zip files, so remove it.

1

This is much more suited for a for loop than a while loop. This way you can also get rid of the needless saving of results from find -iname '*.zip' > zipindex

Do something like this instead:

#!/bin/bash

for zip in $(find -iname '*.zip'); do
    unzip -o $zip
done

This will iterate through all of the lines that find produces.

Of course, you should just dispense with the bash script altogether and make a find oneliner like so:

find -iname '*.zip' -execdir unzip {} \;

EDIT: Thanks to @don_crissti and @ilkkachu, I've managed to get rid of calling another instance of the shell in -exec. -execdir is good to know!

user6075516
  • 898
  • 6
  • 11
1

Using GNU Parallel you could do this:

find my_dir | grep -E '\.zip$' | parallel unzip
inotifywait -qmre MOVED_TO -e CLOSE_WRITE --format %w%f my_dir | grep  -E '\.zip$' | parallel -u unzip

This way you do not need to busy wait for the next zip file to show up.

Ole Tange
  • 35,514