Using grep to find string in files

Using grep to find string in files

How many times I had to search for something, let’s say my mac address, to change it in all files under /etc ? I couldn’t remebmer, different distros – different files, anyway, that’s an example, there are many out there, like searching log files, strings like IPs or titles and the worst part, recursively. I will show few examples of Using grep to find string in files and recursively too.

 

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 "text string to search” directory-path

OR

grep [option] "text string to search” directory-path

OR

grep -r "text string to search” directory-path

OR

grep -r -H "text string to search” directory-path

OR

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

OR

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

Examples

In this example, search for a string called ‘redeem reward’ in all text (*.txt) files located in /home/tom/ directory, use:
$ grep "redeem reward" /home/tom/*.txt
OR
$ grep "redeem reward" ~/*.txt

Task: Search all subdirectories recursively

You can search for a text string all files under each directory, recursively with -r option:
$ grep -r "redeem reward" /home/tom/
OR
$ grep -R "redeem reward" /home/tom/

Task: 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 "redeem reward" /home/tom
Sample outputs:

To just display the filename use the cut command as follows:
$ grep -H -R vivek /etc/* | cut -d: -f1
Sample outputs:

 

Task: 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

Task: 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 ‘getMyData()’ only in ~/projects/ dirctory:
$ grep -w -R 'getMyData()' ~/projects/

Task: Search for two or more words

Use the egrep command as follows:
$ egrep -w -R 'word1|word2' ~/projects/

Task: 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 'getMyData()' ~/projects/ 2>/dev/null

Task: Display matched text in color

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

 

Task: Ignore case (my favourite since the last grep update)

Our final example ignore case distinctions in both the search PATTERN and the input files:
grep -s -R 'word' /path/to/dir
grep -s -r 'income tax' ~/accounting/

 

or just

 

grep -sR ‘word’ /path/

 

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
LUKS List available methods of encryption for LUKS
Viewed 2554 times since Fri, Jul 13, 2018
How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD find
Viewed 3292 times since Mon, Oct 29, 2018
RHEL: Reserved space on a ext2/ext3/ext4 filesystem
Viewed 3875 times since Sun, May 27, 2018
Transform XML to CSV Format | Unix String Pattern Manipulation The Ugly Way
Viewed 3855 times since Sun, Jan 9, 2022
YUM CRON RHEL7: Configure automatic updates.
Viewed 1611 times since Fri, Oct 26, 2018
Using Official Redhat DVD as repository
Viewed 10693 times since Mon, Oct 29, 2018
LVM: Mount LVM Partition(s) in Rescue Mode
Viewed 4155 times since Sat, Jun 2, 2018
How to remove CTRL-M (^M) characters from a file in Linux
Viewed 1937 times since Thu, Feb 7, 2019
OEL 7 – How to disable IPv6 on Oracle Linux 7 – Follow Up
Viewed 9001 times since Wed, Jul 25, 2018
6 rsync Examples to Exclude Multiple Files and Directories using exclude-from
Viewed 4164 times since Wed, Oct 31, 2018