Home > Linux/Unix > Command for Finding a File containing a particular text string

Command for Finding a File containing a particular text string

January 2, 2011

>grep command form (syntax)

grep “text string to search” directory-path

Examples

For example search for a string called redeem reward in all text files located in /home/tom/*.txt directory, use

$ grep “redeem reward” /home/tom/*.txt

Task: Search all subdirectories recursively

You can search for a text string all files under each directory, recursively with -roption:

$ grep -r “redeem reward” /home/tom

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 “redeem reward” /home/tom

Advertisements
Categories: Linux/Unix
%d bloggers like this: