简体   繁体   中英

Run script on files in all levels of sub directories

I have a script written in bash to convert flac files in a folder. However, it doesn't work if the files are in a sub directory of the folder I'm calling.

For example, if I call a directory that has the flac files directly in it, the script works fine. If there are folders between the folder called and the flac files, it wont run.

  • AlbumName/*.flac <-- runs fine when calling AlbumName

  • AlbumName/CD1/*.flac <-- wont run when calling AlbumName

Here is the code I'm using:

for file in "$1"/*.flac
do
    if [ -f "${file}" ]; then

        # do flac conversion script

    fi
done

Any thoughts?

you can get all .flac files with find and then execute the flac conversion script with each one of them passed as argument:

find . -path "*.flac" -type f -exec ./flac_converter.sh "{}" \;

this command finds every file ( -type f ) in path . that matches *.flac at the end of the string, then executes -exec flac_converter.sh with each match {} .

so for example if i have this file structure:

$ tree flacs
flacs
    ├── asd.flac
    ├── dsa.flac
    └── more
        └── more_asd.flac

running that command will produce:

$ find . -path "*.flac" -type f -exec ./flac_converter.sh "{}" \;
I'm converting ./flacs/asd.flac
I'm converting ./flacs/dsa.flac
I'm converting ./flacs/more/more_asd.flac

flac_converter.sh only echoes that sentence with $1 (the file from find as input). notice that it runs ./flacs/more/*.flac as well as ./flacs/*.flac smoothly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM