PingTool.org

Search recursively in file contents

There is a very useful command to search for a particular string in file in bash. It will search subdirectories as well and display a full path to the file, containing the string.
[code]
find . -type f -exec grep -Hn “emacs” {} \; 2>/dev/null
[/code]
Output:
./anaconda-ks.cfg:56:emacs
./install.log:282:Installing emacs-leim-21.4-24.el5.x86_64
./install.log:592:Installing emacs-common-21.4-24.el5.x86_64
./install.log:781:Installing emacs-21.4-24.el5.x86_64
./install.log:792:Installing emacspeak-23.0-3.el5.noarch

The 2>/dev/null argument will hide annoying errors like grep: line too long.
If you want to limit the depth of subdirectories, use -maxdepth X argument. Where X is >0.

For a search in current directory, grep alone is sufficient:

[code]
grep -Hn “string” *
[/code]

Exit mobile version