views:

355

answers:

6

I have a useful little command that searches through all files & subdirectories for a particular string:

find . -name "*" -exec grep -il "search term" {} \;

I'd like to turn this into a function that I can put in my .bashrc and call from my shell without needing to remember the whole thing, but I can't get it working. This is what I have so far:

function ff() { find . -name "*" -exec grep -il $@ \{\} \\\; }

Can anyone help? Many thanks!

+4  A: 
alias ff='grep -irl'

does the same thing but is much simpler and stops unneeded process creation for every file.

Mike McQuaid
Looks good to me :). Why I was using find in there is lost in the mists of time... - thanks!
ajborley
Doesn't work in Solaris which doesn't appear to have the recursive option for grep. Find is usually the way to go in that instance.
Jim
+2  A: 

Well, you can do this:

function ff() { find . -name "*" -exec grep -il $@ {} ';'; }

But that's nonsensical, not only because of grep -r as Mike Arthur points out, but because the clause -name "*" has the exact same effect as nothing at all. What I'd recommend if you're going to do things like this at all is:

function ff() { find . -type f -exec grep -il $@ {} ';'; }
chaos
+2  A: 

Chances are, you'd like the ack utility. It is somewhat like the function you described and more powerful. It is available in Debian and Ubuntu repositories with the name "ack-grep".

maksymko
A: 

Try this:

 $ ff ()  {      find . -name "*" -exec grep -il $@ {} \;; }
Barun
+1  A: 

find -exec has a limited buffer, and is technically slower, than using xargs.

alias ff='find . | xargs grep -il'

will work better when you are searching a large number of files.

A: 

Alternatively, you can use ack. It will help a lot if you're using source control.

ack -il <term>
    <=>
grep -ril <term> .
Rodrigo Queiro