Find all files containing a specific string

David Y.

The Problem

Using Linux, how can I find all files containing a specific string of text?

The Solution

You can do this using grep, a search utility built into most distributions of Linux. For example, to search for the string “hello world” in the files stored under /home/alice, you can use this command:

grep -rnw "hello world" /home/alice
  • -r means search recursively, so grep will exhaustively search through all subdirectories in /home/alice.
  • -n means provide the line numbers of matches.
  • -w means match whole words. This means that the search will not match constructions like “ghello worldy”.

This command should provide output like the following, specifying the file, the line number, and the matched line:

/home/alice/helloworld.py:3:print("hello world")

To learn more about grep, open its manual page by entering man grep.

Join the discussionCome work with us
Share on Twitter
Bookmark this page
Ask a questionImprove this Answer

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedin
© 2023 • Sentry is a registered Trademark
of Functional Software, Inc.