Search code examples
bashloopsls

Bash find all zip files within subfolders, bind files and their path into loop and perform some tasks


I need to write script on Bash, which will find all zip files within subfolders, bind files and their path into some file, and then loop through this list and perform some tasks with all zip files (e.g. extract, check files within zip and then delete extracted files).

Some thoughts:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR   ##current dir
ls $DIR\*.zip

Then bind result to file (ziplist.txt for example) Then read this file in loop per string:

if [[ some result ]] ; then
while IFS= read -r ; do

done <$DIR/ziplist.txt

How this can be done in best way? Sorry, I've have limited experience with bash.


Solution

  • This should do the trick :

    for filename in $(find . -name '*.zip'); do
        # Your operations here
    done
    

    If you want to keep using a while you can do:

    while IFS= read -r ; do
    
    done < <(find . -name '*.zip')