One of the easiest and fastest methods of locating text contained 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”.

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.

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:

  • Computer file help and support. youtube.htm:

    YouTube help and support

    zext.htm:

    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.

    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.

    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 ().

    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.

    • See the grep command page for additional information and help with this command.
    • Linux help and support.