Date Added: 25/10/2017

Linux command Grep

Examples 1

find files including "search words"
find ./ -type f -exec grep -H 'search words' {} \;

Examples 2

grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R is recursive,
-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.

Also, --exclude, --include, --exclude-dir or --include-dir flags exist for narrow searching:

Code below will search in files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

Code below will exclude searching all the files with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

for include or exclude directory(folder), use --exclude-dir and --include-dir parameter.

For example, the code below will not search files in directories dirA/, dirB/ and all of them matching *.dst/:
grep --exclude-dir={dirA,dirB,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

Example 3

only recursive search
grep -r "word" /path/to/dir

All code above worked and tested.

Last Update: Posted by: müslüm ÇEN

Not Commented Yet !
Please login in order to comment . Login