How To Iterate Over FileNames With Spaces

This snippet will iterate over file names containing spaces in a directory.

1
2
3
4
5
echo /my/**/dir | while IFS= read -r file
do
    echo "Doing some work with ${file}"
    ls "${file}"
done

This snippet will iterate over files that contain a string found by grep.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SCHEMA="newschema"
function listFiles {
    grep -lRi OLDSCHEMA \
       /dir1 \
       /dir2
}

listFiles | while IFS= read -r file
do
    echo "Replacing OLDSCHEMA with ${SCHEMA^^} in ${file}"
    # ^ upercases the first letter, ^^ upper cases them app
    sed-i "s/CORPDBA/${SCHEMA^^}/g" "${file}"
done