Recursively iterating through files in a directory can easily be done by:
find . -type f -exec bar {} \;
However, the above does not work for more complex things, where a lot of conditional branches, looping etc. needs to be done. I used to use this for the above:
while read line; do [...]; done < <(find . -type f)
However, it seems like this doesn't work for files containing obscure characters:
$ touch $'a\nb'
$ find . -type f
./a?b
Is there an alternative that handles such obscure characters well?
Best Answer
Yet another use for safe
find
:(This works with any POSIX
find
, but the shell part requires bash. With *BSD and GNU find, you can use-print0
instead of-exec printf '%s\0' {} +
, it will be slightly faster.)This makes it possible to use standard input within the loop, and it works with any path.