Jump to content

Help with the 'find' command


JarDo

Recommended Posts

I'm working on a shell script.  One of the things it's supposed to do is traverse a directory tree looking for files whose name is unknown:

 

	
cd /some/dir/
find . -type f -name '*' -print | while read i 	
do
   if grep $SOMETHING $i > /dev/null
   then
   echo "Do something with file:" $i
fi
done

 

The problem I'm having is that when 'find' stumbles on a file with spaces in its name it returns only the first word in the name (up to the first space).  Then, the subsequent grep trips up because it is trying to act on a filename that doesn't exist.

 

Does anyone know how to deal with finding files, as I am doing above, taking into account the possibility of filenames with spaces?

Link to comment

I'm working on a shell script.  One of the things it's supposed to do is traverse a directory tree looking for files whose name is unknown:

 

	
cd /some/dir/
find . -type f -name '*' -print | while read i 	
do
   if grep $SOMETHING $i > /dev/null
   then
   echo "Do something with file:" $i
fi
done

 

The problem I'm having is that when 'find' stumbles on a file with spaces in its name it returns only the first word in the name (up to the first space).  Then, the subsequent grep trips up because it is trying to act on a filename that doesn't exist.

 

Does anyone know how to deal with finding files, as I am doing above, taking into account the possibility of filenames with spaces?

 

My guess is that you are going to have to use regular expressions and search for things between two forward slashes to define the folder names into a variable that way.  Then you might need to tr " " "\ " the variable to be able to use them properly.

 

and watch your quoting.. it could cause bring up a whole set of problems on their own.

 

Cheers,

Matt

Link to comment

You need to use -print0 and xargs -0 or use -exec

 

 

I'm working on a shell script.  One of the things it's supposed to do is traverse a directory tree looking for files whose name is unknown:

 

	
cd /some/dir/
find . -type f -name '*' -print | while read i 	
do
   if grep $SOMETHING $i > /dev/null
   then
   echo "Do something with file:" $i
fi
done

 

The problem I'm having is that when 'find' stumbles on a file with spaces in its name it returns only the first word in the name (up to the first space).  Then, the subsequent grep trips up because it is trying to act on a filename that doesn't exist.

 

Does anyone know how to deal with finding files, as I am doing above, taking into account the possibility of filenames with spaces?

Link to comment

find . -type f -name '*' -print | while read i

 

Will print the filename and store all of it into i correctly because i is the only variable in the read command

 

however, because $i may contain spaces or special characters you need to double quote it here

 

>>    if grep $SOMETHING "$i" > /dev/null

 

and here

>>    echo "Do something with file:" "$i"

 

 

Also you may not need the -name "*" as the default i

 

you can also do something like

 

find . -type f -exec grep -l "$SOMETHING" {} \;  | while read i

do   "Do something with file " "$i"

done

 

The $i following the   read i   must be double quoted to be resolved by the shell as a whole string without being interpreted by the shell.

 

 

Link to comment

Thank you, everyone.  The hardest part for me with shell scripting is when to use ``, or "", or '', or whatever else to ensure that my variables expand correctly.  I've survived by trial-&-error, but I'd love to find a good website (or even better a book) that explains it once and for all.  It's about as confusing & unclear to me as split-levels in unraid.

Link to comment

Thank you, everyone.  The hardest part for me with shell scripting is when to use ``, or "", or '', or whatever else to ensure that my variables expand correctly.  I've survived by trial-&-error, but I'd love to find a good website (or even better a book) that explains it once and for all.  It's about as confusing & unclear to me as split-levels in unraid.

 

Here are some of the links I used to learn/reference.

 

http://bash-hackers.org/wiki/doku.php/syntax/quoting

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html

http://www.mpi-inf.mpg.de/~uwe/lehre/unixffb/quoting-guide.html

 

Cheers,

Matt

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...