Bash Script - iterating over output of find

后端 未结 3 653
既然无缘
既然无缘 2020-12-02 01:08

I have a bash script in which I need to iterate over each line of the ouput of the find command, but it appears that I am iterating over each Word (space delimited) from the

3条回答
  •  鱼传尺愫
    2020-12-02 02:02

    folders=`foo`
    

    is always wrong, because it assumes that your directories won't contain spaces, newlines (yes, they're valid!), glob characters, etc. One robust approach (which requires the GNU extension -print0) follows:

    while IFS='' read -r -d '' filename; do
      : # something with "$filename"
    done < <(find . -maxdepth 1 -type d -print0)
    

    Another safe and robust approach is to have find itself directly invoke your desired command:

    find . -maxdepth 1 -type d -exec printf '%s\n' '{}' +
    

    See the UsingFind wiki page for a complete treatment of the subject.

提交回复
热议问题