How can I do foreach *.mp3 file recursively in a bash script?

后端 未结 5 1340
你的背包
你的背包 2020-12-09 19:53

The following works fine within the current folder, but I would like it to scan sub folders as well.

for file in *.mp3

do

echo $file

done
5条回答
  •  春和景丽
    2020-12-09 20:31

    This works with most filenames (including spaces) but not newlines, tabs or double spaces.

    find . -type f -name '*.mp3' | while read i; do
       echo "$i"
    done
    

    This works with all filenames.

    find . -type f -name '*.mp3' -print0 | while IFS= read -r -d '' i; do
       echo "$i"
    done
    

    But if you only want to run one command you can use xargs example:

    find . -type f -name '*.mp3' -print0 | xargs -0 -l echo
    

提交回复
热议问题