The xargs approach is the tried and tested way. If you find it too long-winded, you can easily write a little script, put it in your PATH. An alternative is to write a shell function and put it in your .bashrc file
Code:
rgrep () {
if [ $# -ne 2 ]; then
echo "Usage: rgrep pattern directory/[filepattern]"
return 1
fi
file_pattern=""
dir="."
if [ -d "$2" ]; then
dir="$2"
else
case "$2" in
*/*)
# looks like we have a pattern in a directory
file_pattern="${2##*/}"
dir="${2%/*}"
;;
*)
# looks like we only have a pattern (no directory)
file_pattern="$2"
;;
esac
fi
if [ "$file_pattern" != "" ]; then
file_pattern="-name \"$file_pattern\""
fi
find "$dir" -type f $file_pattern -print0 |xargs -0 grep -H "$1"
}
Which you can use like this:
Code:
rgrep pattern /some/existing/dir
Which will searh for pattern in all files under /some/existing/dir and it's sub-directories.
Or like this:
Code:
rgrep pattern /some/place/*.txt
Which will search for pattern in all files whose name ends with ".txt" in /some/place or any of it's sub-directories.