Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jan 11 '13 at 3:12

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

add comment

3 Answers

up vote 3 down vote accepted

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
share|improve this answer
    
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
add comment

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
share|improve this answer
add comment

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

for each in /home/*/.mozilla/firefox/*/places.sqlite
do echo "${each}"
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.