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/"hello world")

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

Loved by over 4 million developers and more than 90,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.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

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

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