Find/Search anything using ‘grep -nri’ command in Linux
Recently i have came across to find pattern in whole directory in Linux server. So for do that i have use a recursive search with the grep command in Linux.
grep command in Unix/Linux
The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for globally search for regular expression and print out).
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
Solution: find + grep
For years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a grep pattern:
find . -type f -exec grep -l 'alvin' {} \;
This command can be read as, “Search all files in all subdirectories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all subdirectories that match the pattern I specify.
Solution: grep -r
However, I was just reminded that a much easier way to perform the same recursive search is with the -r flag of the grep command:
grep -rl alvin .
As you can see, this is a much shorter command, and it performs the same recursive search as the longer command.
If you haven’t used commands like these before, to demonstrate the results of this search, in a PHP project directory I’m working in right now, this command returns a list of files like this:
./index.tpl
./js/jquery-1.6.2.min.js
./webservice/ws_get_table_names.php
Search multiple subdirectories
Your recursive grep searches don’t have to be limited to just the current directory. This next example shows how to recursively search two unrelated directories for the case-insensitive string “alvin”:
grep -ril alvin /home/cato /htdocs/zenf
In this example, the search is made case-insensitive by adding the -i argument to the grep command.
Using egrep recursively
You can also perform recursive searches with the egrep command, which lets you search for multiple patterns at one time. Since I tend to mark comments in my code with my initials (“aja”) or my name (“alvin”), this recursive egrep command shows how to search for those two patterns, again in a case-insensitive manner:
egrep -ril ‘aja|alvin’ .
Note that in this case, quotes are required around my search pattern.
Summary: grep -r
notes
A few notes about the grep -r command:
This particular use of the grep command doesn’t make much sense unless you use it with the -l (lowercase "L") argument as well. This flag tells grep to print the matching filenames.
Don’t forget to list one or more directories at the end of your grep command. If you forget to add any directories, grep will attempt to read from standard input (as usual).
As shown, you can use other normal grep flags as well, including -i to ignore case, -v to reverse the meaning of the search, etc.
Here’s the section of the Linux grep man page that discusses the -r flag:
-R, -r, –recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.
–include=PATTERN
Recurse in directories only searching file matching PATTERN.
–exclude=PATTERN
Recurse in directories skip file matching PATTERN.
As you’ve seen, the grep -r command makes it easy to recursively search directories for all files that match the search pattern you specify, and the syntax is much shorter than the equivalent find/grep command.
For more information on the find command, see my Linux find command examples, and for more information on the grep command, see my Linux grep command examples.