Articles & Code Snippets

Finding a text string in Linux

Finding a text string in Linux

In this example, search for a string called "jquery-1.6.2" in all text (*.js) files located in "/home/user/public_html/" directory, use:
$ grep "jquery-1.6.2" /home/user/public_html/*.js
$ grep "jquery-1.6.2" ~/*.js

Search all subdirectories recursively

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

$ grep -R "jquery-1.6.2" /home/user/public_html/

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 "jquery-1.6.2" /home/user/public_html

Finding text within files

[email protected]:~# rgrep '' /var/lib/bind/*.hosts
[email protected]:~# grep -r '' /var/lib/bind/*.hosts