2

Currently, I am trying to parse out all the files inside a directory. I have a find function, but it appears to not be able to parse in file directories with whitespaces into a loop. Here, "$DIR" is the directory I wish to search in. Does anyone have any ideas how I can modify it to work? thanks.

for file in $(find "$DIR" -type f)
do
   echo "$file"
done

3 Answers3

5

You don't need to for loop this.

find "$dir" -type f

will by default output all found objects.

You can make this explicit with:

find "$dir" -type f -print

If you really want to iterate over these, use a null separator and xargs:

find "$dir" -type f -print0 | xargs -0 -n1 echo

Or find's own -exec:

find "$dir" -type f -exec echo "{}" \;
DopeGhoti
  • 76,081
5

find ... | xargs ... and find ... -exec ... are both better options than this: to use a shell loop to iterate over find results correctly, we must use a while read loop:

while IFS= read -d '' -r filename; do 
    echo "$filename"
done < <(find "$dir" -type f -print0)

A lot to unpack there:

  • we use a Process Substitution to execute the find command and be able to read from the results like it's a file.
  • To read a line of input verbatim, the bash idiom is IFS= read -r line. That allows arbitrary whitespace and backslashes to be read into the variable.
  • read -d '' uses the null byte (as produced by -print0) as the end-of-line character instead of newline.
glenn jackman
  • 85,964
4

You should not parse the output of find for the same reasons you shouldn't parse ls

Try using find/exec instead:

find "$DIR" -type f -exec echo {} \;
Kusalananda
  • 333,661
jesse_b
  • 37,005