How to find all files with names containing a string on Linux?

In Linux command line, finding all files with names containing a specific string is accomplished using the grep command. The grep command is a powerful text processing utility that searches for patterns within files and displays matching lines.

The grep command filters searches in files for particular patterns of characters. It is one of the most frequently used Linux utilities for displaying lines that contain the pattern you are searching for. The pattern being searched is typically referred to as a regular expression.

Syntax

grep [options] pattern [files]

Common grep Options

-c : Lists only a count of the lines that match a pattern
-h : Displays the matched lines only
-i : Ignores case for matching
-l : Prints filenames only
-n : Display the matched lines and their line numbers
-v : Prints out all the lines that do not match the pattern
-R : Stands for recurse, searches subdirectories as well
-s : Suppress error messages about nonexistent or unreadable files

Finding Patterns in All Files (Including Subdirectories)

To search for a pattern in all files within a directory and its subdirectories, use the following syntax −

grep -rni "word" *

Replace "word" with your search string. The flags used are −

  • -r − Recursive search through subdirectories

  • -n − Show line numbers

  • -i − Case-insensitive search

Example − Searching for "func main()"

grep -rni "func main()" *

This command searches for the string "func main()" in all files within the current directory and subdirectories.

main.go:120:func main() {}

Finding Patterns in Current Directory Only

To search only in the current directory without descending into subdirectories, use the -s flag −

grep -s "func main()" *

The -s flag suppresses warning messages for subdirectories, focusing the search on files in the current directory only.

main.go:120:func main() {}

Comprehensive Example

Let's search for the word "apples" in all files recursively from the current directory −

grep -R "apples" .

The dot (.) represents the current directory. This command will find all occurrences of "apples" in any file within the directory tree.

./d1/file.txt:apples
./d1/file.txt:applesauce
./d1/file.txt:applesnits
./d1/file.txt:dapples
./d1/file.txt:grapples
./d1/file.txt:mayapples
./d1/file.txt:pineapples
./d1/file.txt:sapples
./d1/file.txt:scrapples
./d2/2.txt:orange apples is great together
./d2/2.txt:apples nto great
./d2/2.txt:is apples good

Common Use Cases

Command Purpose
grep -r "error" /var/log/ Find all occurrences of "error" in log files
grep -l "TODO" *.js List JavaScript files containing "TODO"
grep -rni "password" . Case-insensitive search for "password" with line numbers
grep -v "debug" app.log Show lines NOT containing "debug"

Conclusion

The grep command is an essential tool for finding files containing specific strings in Linux. Using options like -r for recursive search, -i for case-insensitive matching, and -n for line numbers makes it highly versatile for system administration and development tasks.

Updated on: 2026-03-17T09:01:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements