5

I'm trying to grep multiple extensions within the current and all sub-folders.

grep -i -r -n 'hello' somepath/*.{php,html}

This is only grepping the current folder but not sub-folders.

What would be a good way of doing this?

innaM
  • 47,505
  • 4
  • 67
  • 87
Mike
  • 53
  • 1
  • 3

3 Answers3

11

Using only grep:

grep -irn --include='*.php' --include='*.html' 'hello' somepath/
Bryan Head
  • 12,360
  • 5
  • 32
  • 50
  • 1
    That's the neater solution. Is there a possibility to munge the include patterns together? I messed around with glob patterns, but never got it to work correctly. :-( – Christoph Jan 12 '13 at 13:50
2

One of these:

find '(' -name '*.php' -o -name '*.html' ')' -exec grep -i -n hello {} +
find '(' -name '*.php' -o -name '*.html' ')' -print0 | xargs -0 grep -i -n hello
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

I was looking the same and when decided to do a bash script I started with vim codesearch and surprise I already did this before!

#!/bin/bash
context="$3"
#ln = line number mt =  match mc = file
export GREP_COLORS="sl=32:mc=00;33:ms=05;40;31:ln="
if [[ "$context" == "" ]]; then  context=5; fi
grep --color=always -n -a -R -i -C"$context" --exclude='*.mp*'\
 --exclude='*.avi'\
 --exclude='*.flv'\
 --exclude='*.png'\
 --exclude='*.gif'\
 --exclude='*.jpg'\
 --exclude='*.wav'\
 --exclude='*.rar'\
 --exclude='*.zip'\
 --exclude='*.gz'\
 --exclude='*.sql' "$2" "$1" | less -R

paste this code into in a file named codesearch and set the chmod to 700 or 770 I guess this could be better here for the next time that I forgot

this script will show with colors the matches and the context around

./codesearch '/full/path' 'string to search' 

and optional defining the number of context line around default 5

./codesearch '/full/path' 'string to search' 3

I edited the code and added some eye candy

example ./codesearch ./ 'eval' 2 screenshot codesearch

Looks like this when you have enabled "allow blinking text" in terminal