Suppose I want to unzip files recursively that have the pattern ${file}*.zip in some directory /home/A/XML using find command

find /home/A/XML -type f -name "${file}*.zip" -exec unzip '{}' -d /target/path \;

For now, I wish to parse a list of directories instead of just /home/A/XML

for dir in "${path}"; do        

    find "${dir}" -name "${file}*\.zip" -type f -exec unzip '{}' -d "${dir}"/"${file}" \;


I tries using a for loop to parse the directories but the unzip stop right after the 1st directories, meaning it done the same thing as the previous code, it only unzipped in the directory /home/A/XML.

caution:  not extracting; -d ignored

I got the error above for the remaining directories. Can anyone please guide me on this? Thanks in advance.


If that list of directories is stored newline-delimited (one on each line) in a scalar variable $path (not a good name for a variable as it's special in several shells, though not bash), you'd do:

IFS=$'\n' # split on newline
set -o noglob # disable glob

for dir in $path # invoke the split+glob operator ($path unquoted)
  find "$dir" -name "$file*.zip" -type f -exec unzip -d "$dir/$file" {} \;

If $path was a bash array instead as in path=(dir1 dir2...), change the loop to

for dir in "${path[@]}"

If using GNU find, you may want to add a -quit predicate at the end or your find command to allow only one successful unzip per directory unless you want all the zip files to be merged into the same directory.

Note that options should be placed before non-option arguments.

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.