views:

116

answers:

4
+1  Q: 

Bash alias query

How can I turn the following command into a bash alias?

find . -name '*.php' | xargs grep --color -n 'search term'

Where I can specify the file extension and 'search term' is obviously the search term :)

So what I want to do is:

 searchFiles 'php' 'search term'

How do I pass the input into the alias? Should I just create a bash script and have the alias point to the script?

+3  A: 

How about using a function? Add this to your .bashrc:

function searchFiles() {
       find . -name \*."$1" -print0 | xargs -0 grep --color -n "$2"
}

and then use it like:

$ searchFiles c include

Gonzalo
Perfecto - thanks mate
ae
As **Chris Johnsen** states, you should use `-print0` and `-0`
Dennis Williamson
Updating answer...
Gonzalo
+3  A: 

You could use an alias, but using a function, like Gonzalo shows, the sane thing to do.

alias searchFiles=sh\ -c\ \''find . -name \*."$1" -type f -print0 | xargs -0 grep --color -Hn "$2"'\'\ -

Whether function or alias, I recommend using -print0 with find and -0 with xargs. This provides for more robust filename handling (most commonly, spaces in filenames).

Chris Johnsen
+1 excellent ....
Ewan Todd
+1  A: 

While a function works, it won't be callable from other programs and scripts (without a lot of pain). (An alias would have the same problem.) A separate script would be my choice, since it sounds like you want to invoke it directly:

#!/bin/bash
# since you're already using bash, depend on it instead of /bin/sh
# and reduce surprises later (you can always come back and look at
# porting this)

INCLUDE="*.$1"
PATTERN="$2"
grep --color -n "$PATTERN" --recursive --include="$INCLUDE" .

(No need for find with what you have.)

If this was only used inside another script instead of directly, a function would be easier.

Roger Pate
You can `export` a function to make it available to other scripts. If there are a large number of files it may make sense to use `find` and `xargs`. I don't see anything in your script that's Bash-specific. I think the Bourne shell would be perfectly happy with it.
Dennis Williamson
Yes, it's possible, but it's not as easy for someone unfamiliar to grasp than just executing a script. Why do you think find+xargs would be better? Re sh, I was just pointing out that he doesn't have to worry about "bash vs sh" (which can be a PITA) just because he's writing a separate script---just write a bash script.
Roger Pate
I agree with @Roger. Assuming you have your own private "~/bin" directory in your path in which to keep it (you do, don't you? :), a stand-alone script is always more flexible and robust for little commands you want to use both directly in the cli and inside other scripts. A directory of small sripts is easier to deal with than a single file full of unrelated functions, IMHO.
Kevin Little
A: 

Shell functions are good, but you may also want to take a look at ack. It handles file-specific searching, with color-coding, so that your example would simply be

ack --php somepattern
Andy Lester