Wednesday, September 23, 2009

search for inside all files in current directory with grep

grep -RnisI <pattern> *
-R: recurse to subdir
-n: show line number
-i: ignore case
-s: suppress “doesn’t exist” and “cannot read” message
-I: ignore binary files

another one:

find . -type f | xargs grep -RnisI <pattern>

--[update: 11/3/09]
grep --color -RnisIH 'pattern' .
-H: show file name
--color: highlight the word (or use --color=auto, --color=always)

find . -type f -name '*.c' -print0 | xargs -0 --color -RnisIH 'search_pattern'

--if we want to find all 'oldstring' and replace with 'newstring':
grep -Rl 'oldstring' . | sed -i -e 's/oldstring/newstring/'
-l: only list the file name with containing matches.
Refer former post

No comments:

Post a Comment