text
string
linux

Find all files containing a specific text (string) on Linux?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

On Linux, the standard answer is usually grep, and the fastest modern answer is often rg, short for ripgrep. Both tools can find files containing a string, but the best command depends on whether you want matching lines, matching file names only, recursion, case-insensitive search, or filtering by file type.

The Basic grep Pattern

To search recursively under the current directory and print matching lines:

bash
grep -R "search_string" .

To print only the file names that contain at least one match:

bash
grep -Rl "search_string" .

Those two commands already solve most day-to-day cases.

Useful flags include:

  • '-R for recursive search'
  • '-l for files with matches only'
  • '-n for line numbers'
  • '-i for case-insensitive matching'
  • '--include and --exclude for file filtering'

For example, search only Python files and show line numbers:

bash
grep -Rni --include='*.py' "search_string" .

Use rg When It Is Available

rg is usually faster and more convenient than grep for source trees.

bash
rg "search_string"

That command searches recursively from the current directory by default. To print file names only:

bash
rg -l "search_string"

To search only JavaScript files:

bash
rg -l -g '*.js' "search_string"

One reason developers like ripgrep is that it respects ignore files such as .gitignore automatically, which keeps searches focused on the code rather than generated output or dependency folders.

When find Helps

Sometimes the search problem is really two problems:

  1. pick a specific set of files
  2. search only inside that set

That is when find becomes useful.

bash
find /var/log -type f -name '*.log' -exec grep -l "timeout" {} +

This command says:

  • start in /var/log
  • keep only regular files ending in .log
  • run grep on that selected set

This approach is more explicit than a broad recursive search when directory shape or file selection is part of the requirement.

Fixed Strings Versus Regular Expressions

By default, grep and rg interpret the pattern as a regular expression. If you want a literal string, use fixed-string mode.

With grep:

bash
grep -RFl "error[42]" .

With rg:

bash
rg -F -l "error[42]"

This matters whenever the text contains characters such as ., *, [, or ? that have special regex meaning.

Binary Files and Large Trees

Search tools may encounter binary files, archives, or huge dependency folders. If the goal is source code or logs, filter early.

For example, with grep you can exclude directories:

bash
grep -RIl --exclude-dir=node_modules --exclude-dir=.git "search_string" .

With ripgrep, ignore rules usually help automatically, but explicit globs are still useful when you need tighter control.

Common Pitfalls

Forgetting -l is a common mistake when you want file names only. Without it, grep prints matching lines instead.

Treating the search text as literal when the tool is reading it as a regular expression can also produce confusing matches. Use fixed-string mode when needed.

Searching huge trees without excludes is another common performance problem. Generated files, dependency folders, and binaries add noise and cost.

Finally, if you are searching source code, prefer rg when it is installed. It is usually the better tool for that workflow.

Summary

  • use grep -Rl "text" . to list files containing a string recursively
  • use grep -Rni when you also need case-insensitive matching and line numbers
  • use rg for a faster, code-oriented search experience when it is available
  • combine find with grep when file selection is part of the problem
  • use fixed-string mode if the search text contains regex special characters that should be treated literally

Course illustration
Course illustration

All Rights Reserved.