Thursday, January 3, 2013

Finding a File containing a particular text string in Linux server

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

grep command form (syntax)

grep “text string to search” directory-path or ip addtress


For example search for a string called redeem reward in all text files located in /home/guest/*.txt directory, use
$ grep "192.168.xx.xx" /home/guest/*.txt

Task: Search all subdirectories recursively

You can search for a text string all files under each directory, recursively with -roption:
$ grep -r "192.168.xx.xx" /home/guest

Task: Only print filenames

By default, grep command prints the matching lines You can pass -H option to print the filename for each match.
$ grep -H -r “192.168.xx.xx” /home/guest
filename.txt: 192.168.xx.xx
To just print the filename use cut command as follows:
$ grep -H vivek /etc/* -R | cut -d: -f1


No comments:

Post a Comment


Check Page Rank of your Web site pages instantly:

This page rank checking tool is powered by Page Rank Checker service