Dealing with files/folders with spaces in bash script

I need to search for places.sqlite on a system which may have spaces in the folder name; This works without spaces in folder name:

    for each in find /home/ -name "places.sqlite" | grep -i ".mozilla/firefox" ;do
echo "${each}" done  Which prints: /home/itsupport/.mozilla/firefox/d2gigsya.default/places.sqlite (for example) However, if the folder contains spaces it chops up path to the file and breaks my script! To recap, this type of folder WOULD work in the script: $ sudo find /home/ -name "places.sqlite" | grep -i ".mozilla/firefox"
/home/itsupport/.mozilla/firefox/d2gigsya.default/places.sqlite


And this folder with spaces does NOT work in the script:

    $sudo find /home/ -name "places.sqlite" | grep -i ".mozilla/firefox" /home/itsupport/.mozilla/firefox/Random Ass Location/places.sqlite  I know that you can use$(command) or something but I'm not sure what to do when I'm using the find as the loop variable. Maybe that is my mistake here. Anyway, any help would be great.

-

migrated from stackoverflow.comJan 11 '13 at 3:12

This question came from our site for professional and enthusiast programmers.

find has the -print0 flag to deal with this problem:

#!/bin/bash

find . -print0 | while read -d $'\0' file do echo${file}
done


Example:

$ls script.sh space name$ ./script.sh
.
./script.sh
./space name

-
Thanks! This worked for me : find /${1}/ -name "${2}" -print0 | while read -d $'\0' file ;do – Mike Q Jan 11 '13 at 0:03 however the extra grep I had in there didn't work. Figuring that part out now. – Mike Q Jan 11 '13 at 0:04 Another option is to use IFS to split on line end, instead of any whitespace characters. oldIFS="$IFS"
IFS=$'\n' for bla in .... do ... done IFS="$oldIFS" # restoring to avoid surprising the rest of the script

-

Since the files are in well-known locations, you can just use

for each in /home/*/.mozilla/firefox/*/places.sqlite
do echo "\${each}"
done

-