Linux Search Files For Text Or String Recursively
This tutorial will show you how to find or search files for specific text or string in linux command line using command grep
.
Are you trying to find a way to search files containing a specific string of text,which also means to list the list of files which include the string you are searching.
Short answer
grep -Riw "string-to-search-here" /path-to-search-here
A real example:
grep -Riw "host" /etc
Where:
-R
read and process all files in /etc recursively-i
search string case-insensitive, also will match capitalHOST
-w
matches whole word/etc
where to search from
Introduction
grep is a often used command which prints lines that contain a match for one or more patterns. When grep finds a match in a line, it prints the line to standard output.
synopsis :
grep [option...] [patterns] [file...]
grep only search specific files
grep --include='*.{c,txt}' -r 'string-search' /path-to-search
This will only search through those files which have .c or .txt extensions
grep excludes files or directories
grep --exclude='*.{c,txt}' -r 'string-search' /path-to-search
Similar to include
this command will exclude files which have .c or .txt extensions
grep --exclude-dir={dir1,dir2,log*} -r 'string-search' /path-to-search
We can exclude some directories to speed up the searching , above example will exclude the dirs dir1/, dir2/ and all directories whose name start from log.
Grep multiple words or strings
egrep
is a good choice to do this ,egrep is the same as grep -E
which will Interpret PATTERN as an extended regular expression.
$ egrep 'root|nobody' /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
Or use grep with option
grep -E 'root|nobody' /etc/passwd
select non-matching lines
Below example will grep out comments line starting with symbol #
and blank line also
$ grep -v -E '^#|^$' /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
-v
means select non-matching contents-E
parse pattern as extended regular expression^#
stands a comment line^$
stands a blank line
Only list the names of matching files
grep -l 'main' test-*.c
lists names of ‘test-*.c’ files in the current directory whose contents mention ‘main’.
Print context around the matching lines
grep -C 5 'word' message*.log
outputs 5 lines of context around each matching line.
force grep
to print the name of the file
use -H
option which will print the file name for each match
$ grep -H -w root /etc/passwd
/etc/passwd:root:x:0:0:root:/root:/bin/bash
Remove “Binary file matches” report
j@ubuntu:~/dir1$ grep word *
abc:hello word ....
Binary file cmd matches
j@ubuntu:~/dir1$ grep -I word *
abc:hello word ....
Option -I
which equals –binary-files=without-match
grep match cross line
With the GNU grep
option -z (–null-data), each input and output “line” is null-terminated.
j@ubuntu:~/dir1$ cat test.txt
hello word hello
world
j@ubuntu:~/dir1$ grep -z 'hello[[:space:]]\+world' test.txt
hello word hello
world
j@ubuntu:~/dir1$
Conclusion
Now we have shown you how to search text or string using command grep in Linux command line , if you still have queries please feel free to leave a comment.