How to find text within files in Linux
This page is for users looking to finding text within files in Linux and not how to find files in Linux. For help with finding files, see: How to find files in Linux and Unix.
One of the easiest and fastest methods of locating text within a file on a computer running Linux is to use the grep command. Below is a basic example of a command used to locate any htm file containing the word "help".
grep "help" *.htm
If any matches are found, text similar to the following example is shown. If no matches are found, nothing is returned, and you'll be placed back at the prompt.
yext.htm: <li><a href="/software/file.htm">Computer file help and support. youtube.htm: <h1 itemprop="headline">YouTube help and support</h1> zext.htm: <meta property="og:image" content="https://www.computerhope.com/cdn/freehelp.jpg"/>
In the above grep example results, grep shows files containing the text we're looking for (help), with the files containing the text at the beginning of the line.
In our grep command example, we used *.htm, a wildcard for any file ending with the .htm file extension. If you wanted to search all files, you could type a single asterisk (*).
Search subdirectories
The above example would only search the current directory. If you wanted to search the current directory and all subdirectories, you'd need to search recursively. To do this, you'd type a command similar to the following example.
grep -r "computerhope" /www/
In the above example, grep searches the /www/ directory and all subdirectories for the text "computerhope" and returns any results found.