The following shows how to search text in files in Linux.
Basic Syntax
grep [options] "text to search" /root/director/name
Options
Options are separated by space
--color: hightlight search phrase and file names -H: return only the file names. Need to specify more. Please read the next topic. -R: search subdirectories -i: ignore case
Return only file names
grep --color -H -R "www.google.com" /root/dir | cut -d: -f1
Unique file names
Add sort -u to get unique file names
grep --color -H -R "www.google.com" /root/dir | cut -d: -f1 | sort -u
Search multiple words
Use pipe to separate the words
grep --color -H -R "google.com|yahoo.com" /root/dir | cut -d: -f1
Not to include \ in text to search
If you include “\” in text, you might trigger a regex search
The following searches text that is:
- Any word that starts with 2012
- 2012 follows any character that is repeated 12 times
grep "\b2012.\{12\}\b" /root/dir
Reference
I compiled my notes based on this great article
The post Search Text in Files in Linux appeared first on Living in IT.