tags:

views:

32

answers:

2

if i want an alias to do "rgrep pattern *" to search all files from my current location down through any sub directories, what is an alias for rgrep I can add to my bashrc file?

i would also like it to ignore errors and only report positive hits

A: 

How about:

alias rgrep="grep -r"

This will only show 'positive hits', i.e. lines that contain the pattern you specify.

Small piece of advice, however: you might want to get used to just using grep -r directly. You'll then find it much easier if you ever need to use someone else's workstation, for instance!

Edit: you want to match patterns in file names, not in their contents (and also in directory names too). So how about this instead:

alias rgrep="find | grep"

By default, find will find all files and directories, so then it's just a case of passing that list to grep to find the pattern you're looking for.

Richard Fearn
I think this will miss directories named with my pattern, eh?
Derek
+1  A: 

In order for it to ignore errors (such as "Permission denied"), you'll probably need to use a function instead of an alias:

rgrep () { grep -r "${@}" 2>/dev/null; }
Dennis Williamson
how would i modify this to make it also find directories? if I have a directory named "hi" and I want to do "rgrep hi *" from some upper level dir I want that hit to come up too
Derek
`grep` doesn't search for file or directory names, and there is no flag you can pass to make it do that. It only looks at file _contents_. You'd need to separately invoke a command like `find . -name` if you want results based on file/dir names to print. It could go in the same function if you really want your weird rgrep thing to do both.
Walter Mundt
@Derek: As Walter said, `find` is probably what you want.
Dennis Williamson