If you have a list of files that you want to operate on in a loop in bash and some of them have spaces in the file name the default IFS (Internal Field Separator) will match with the space and tokenize the file.
The simple approach is to temporarily set the IFS as follows. This can be done in a shell script, but the following example is directly on the command line for ‘one-liner’ usage.
OIFS="$IFS" IFS=$'\n' for i in `find ./ -type f -iname '*some_criteria*'`; do "something with $i"; done IFS="$OIFS"
The previous commands will:
- Save the existing IFS
- Update the IFS to a newline char
- Execute your loop with the results of a find command
- Reset the IFS