How To Find A Particular Text String In Files/Directory Linux

Wednesday, March 1, 2017

How To Find A Particular Text String In Files/Directory Linux :-


There are around thousands of text files on any Linux/Centos/Ubuntu or Unix based server. Need to Find and locate those files can be done with the find command.

You need to use the grep command. The grep command searches the given input Files for lines containing a match or a text string.

grep command syntax

The syntax is:

grep "your search string" directory-path

OR

grep [option] "your search string" directory-path

OR

grep -r "your search string" directory-path

OR

grep -r -H "your search string" directory-path

OR

egrep -R "word-1|word-2" directory-path

OR

egrep -w -R "word-1|word-2" directory-path


All Examples :-

Example 1.
In this example, search for a string called ‘my handler’ in all text (*.txt) files located in /home/user/ directory, use:

$ grep "my handler" /home/user/*.txt

OR
$ grep "my handler" ~/*.txt

Example 2.
Search all subdirectories recursively
You can search for a text string all files under each directory, recursively with -r option:
$ grep -r "my handler" /home/user/

OR
$ grep -R "my handler" /home/user/

Example 3.
Only display filenames
By default, the grep command prints the matching lines. You can pass -H option to print the filename for each match:
$ grep -H -r "my handler" /home/user

Sample outputs:

myfile.txt: my handler
handler.txt: my handler
...
To just display the filename use the cut command as follows:
$ grep -H -R somy /etc/* | cut -d: -f1

Sample outputs:

myfile.txt
handler.txt
......

Example 4.
Suppress file names
The grep command shows output on a separate line, and it is preceded by the name of the file in which it was found in the case of multiple files. You can pass the -h option to suppress inclusion of the file names in the output:
$ grep -h -R 'main()' ~/projects/*.c

Example 5.
Display only words
You can select only those lines containing matches that form whole words using the -w option. In this example, search for word ‘getMyProfile()’ only in ~/projects/ dirctory:
$ grep -w -R 'getMyProfile()' ~/projects/

Example 6.
Search for two or more words
Use the egrep command as follows:
$ egrep -w -R 'word1|word2' ~/projects/

Example 7.
Hide warning spam
grep command generate error message as follows due to permission and other issues:

No such file or directory
No such device or address
Permission denied

To hide all errors or warning message spam generated by the grep command, append 2>/dev/null to grep command. This will send and hide unwanted output to /dev/null device:
$ grep -w -R 'getMyProfile()' ~/projects/ 2>/dev/null

Example 8.
Display matched text in color
Pass the --color option to the grep command display matched text/words in color on the terminal:

grep --color 'word' file
grep --color -R 'word' /path/to/dir
grep --color -R "192.168.1.5" /etc/
grep --color -R -h "192.168.1.5" /etc/
grep --color -R -h "192.168.1.5" /etc/ 2>/dev/null
Sample outputs:

Fig.01: grep command in action with colors and hiding the warnings on screen
Fig.01: grep command in action with colors and hiding the warnings on screen

Example 9.
Ignore case
Our final example ignore case distinctions in both the search PATTERN and the input files:
grep -i -R 'word' /path/to/dir
grep -i -r 'income tax return' ~/accountingfiles/

0 comments:

About This Blog

Lorem Ipsum

  © Copyright 2009 Linux-HelpLine.Blogspot.com

Back to TOP