Searching code files for a particular string
Im using Ubuntu Karmic as my operating system . I frequently need to search my project folder for a particular string, to see if its there in any 开发者_如何转开发of the files in the project folder or its subfolders. I currently use the find command to do that, and have written a script that accepts the string im looking for as the parameter.
find . -exec grep -l $1 {} \;
But the problem with this is that it does not work with strings having a space in them. So, is there any way to search for space separated strings as well, or is there any available tool that does the job ?
Thank You.
How are you invoking your script?
If you want to search for space separated strings you need to do the
invocation in the form:
%./script_name.sh 'search string'
and also change the find invocation to :
find . -exec grep -l "$1" {} \;
A better version of that command is simply grep -rl "$1" .
, or possibly grep -rl "$*" .
.
If your string contains the correct amount of space, and the problem is simply the shell parsing the arguments, then you can refer to every arg with "$*"
and you can prevent the shell from breaking at word boundaries (but still allow parameter expansion) by using the soft double-quotes.
grep -R "phrase with spaces" /folder/folder
find / -type d -name "folder name" 2> /dev/null
I believe you may simply do grep -lR ${1} .
to achieve what you need.
精彩评论