I find myself frequently doing the following:
for f in `find -foo -bar -baz`; do
process "$f"
done
This of course doesn't work for file names with spaces. How can I handle such cases?
I find myself frequently doing the following:
for f in `find -foo -bar -baz`; do
process "$f"
done
This of course doesn't work for file names with spaces. How can I handle such cases?
In such cases, my approach is to build the list before the for
command and replace whitespace inside element names by another character or string which is unlikely to appear.
Then inside the loop, I replace back that specific string by a whitespace.
An example:
list=`find -foo -bar -baz | tr ' ' 'µ'`
for fx in $list ; do
f=`echo $fx | tr 'µ' ' '`
process "$f"
done
If you are using find already, why not simply use exec
find -foo -bar -baz -exec process '{}' \;
The alternative solution would be to change the IFS variable (inter field seperator)
Find and xargs work well together. find can print the names of the files with a \0
-delimiter (option print0
) and xargs can read them in that format (option -0
):
find . -type f -print0 | xargs -0 echo
bash 4
shopt -s globstar
for file in /path/**
do
process "$file"
done