Sentry Answers>Linux>

Find all files containing a specific string

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:

Click to Copy
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:

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

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

  • Syntax.fmListen to the Syntax Podcast
  • ResourcesWhat is Distributed Tracing
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

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