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:
To print only the file names that contain at least one match:
Those two commands already solve most day-to-day cases.
Useful flags include:
- '
-Rfor recursive search' - '
-lfor files with matches only' - '
-nfor line numbers' - '
-ifor case-insensitive matching' - '
--includeand--excludefor file filtering'
For example, search only Python files and show line numbers:
Use rg When It Is Available
rg is usually faster and more convenient than grep for source trees.
That command searches recursively from the current directory by default. To print file names only:
To search only JavaScript files:
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:
- pick a specific set of files
- search only inside that set
That is when find becomes useful.
This command says:
- start in
/var/log - keep only regular files ending in
.log - run
grepon 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:
With rg:
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:
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 -Rniwhen you also need case-insensitive matching and line numbers - use
rgfor a faster, code-oriented search experience when it is available - combine
findwithgrepwhen file selection is part of the problem - use fixed-string mode if the search text contains regex special characters that should be treated literally

