Here's a script:
Alternatively here is a one shot command:
grep -rnw '/path/to/somewhere/' -e 'pattern'
- -r or -R is recursive ; use -R to search entirely
- -n is line number, and
- -w stands for match the whole word.
- -l (lower-case L) can be added to just give the file name of matching files.
- -e is the pattern used during the search
doing this all the time
If you do, then you should add this to your bashrc:
# grep but recursive
grec() {
if [ "$#" -lt 1 ]; then
echo "usage: grec PATTERN [PATH]"
return 1
fi
local pattern="$1"
local path="${2:-.}"
grep -RnI --color=auto "$path" -e "$pattern"
}
Note that I omitted w in the above command because a lot of the time we don't know what the full word is that we're looking for.