To grep multiple files for text, give grep a search pattern followed by the file names: grep 'error' app.log system.log. To search a directory and its subdirectories, use grep -r 'error' logs/; add options such as -n to show line numbers or -i to ignore letter case.
What Grep Does When Searching Files
grep is a command-line tool that looks for lines matching a pattern in text input. It can read one file, several named files, or files reached through a directory search. Its output normally shows each matching line, making it useful for finding a message in logs, locating a setting in configuration files, or checking where a phrase appears in a collection of documents.
When you pass more than one file name, grep searches each file in turn. If matches are found, the output usually includes the file name before each matching line, so you can tell where a result came from. For example, grep 'timeout' server.log worker.log searches both logs. This is different from joining the files yourself: grep keeps the source file identifiable in its results.
Search Several Named Files
List the files after the pattern, separated by spaces. Quoting the pattern is a useful habit, especially when it contains spaces or characters that the shell may interpret specially.
grep 'connection refused' app.log worker.log archive.log
To search every text file in the current directory, a shell wildcard can supply the file names:
grep 'connection refused' *.log
The shell expands *.log into the names of matching entries before it runs grep. This searches files in the current directory only, not files in nested directories. If the wildcard has no matches, what happens depends on the shell; the pattern may be passed literally or produce a shell error.
You can also provide a list of paths, including paths in different directories:
grep 'TODO' src/main.c docs/notes.txt tests/check.py
Use quotes around a file path that contains spaces, such as 'meeting notes.txt'. The quotes tell the shell to pass the path as one argument; they are not part of the file name.
Search Directories Recursively
For a directory tree, use the recursive option -r or -R. The lowercase form follows symbolic links encountered while traversing directories according to grep’s usual behavior; the uppercase form also follows symbolic links to directories. If links may lead outside the tree or create confusing traversal, choose deliberately and consult the installed grep manual for platform-specific details.
grep -r 'TODO' project/
Recursive search is convenient when you do not know which file contains the text. It may also search generated files, dependencies, or large data directories that you did not intend to include. Limit the starting path to the relevant part of the project, or use an include pattern:
grep -r --include='*.py' 'TODO' project/
The include option restricts recursive matches to files whose names fit the supplied pattern. You can use an exclude option to skip a matching file-name pattern. These controls concern file names, not the contents of files; they do not mean that grep understands a programming language or knows which files are source code.
Useful Options for Clearer Results
Options can make a large search easier to read. They are commonly combined, though writing them separately can make a command clearer while learning.
-nprints line numbers alongside matches.-iignores differences between uppercase and lowercase letters.-wmatches a whole word rather than a matching part inside a longer word.-lprints the names of files containing a match instead of displaying every matching line.-creports the number of matching lines per file.-vprints lines that do not match the pattern.-Hrequests file names in output, and-hsuppresses them where grep would otherwise show them.
For instance, grep -in 'warning' *.log searches matching log files without regard to case and shows line numbers. grep -rl 'deprecated' src/ reports the names of files containing the term, which is often more manageable than printing every matching line.
Some grep versions support options that differ from those available on other systems. If a command works on one computer but not another, check the local manual or built-in help rather than assuming every operating system ships the same implementation.
Literal Text, Regular Expressions, and Quoting
By default, grep treats its pattern as a basic regular expression, not always as an ordinary string. A period, for example, can match a character rather than only a literal period. If you want a plain-text search, use -F:
grep -F 'file.name' *.conf
This searches for the exact sequence file.name, including the period. Fixed-string mode is also a good choice for phrases containing punctuation or text copied from an error message. To search for several fixed strings, some grep versions offer -e for each pattern; check the local documentation for supported behavior.
Regular expressions are useful when the target varies. For example, grep -E 'error|failure' *.log uses extended regular-expression syntax to find either word. The vertical bar means “or” in that pattern. Shell quoting protects the pattern from being interpreted by the shell before grep receives it. Single quotes are often convenient for patterns that contain punctuation, though shell quoting rules vary across command environments.
Choose Between File Lists, Wildcards, and Recursion
The right approach depends on whether you know the files in advance and how broad the search should be.
- Use explicit file names when you need a small, known set of files and want to avoid unrelated matches.
- Use a wildcard such as
*.txtfor matching files in one directory. - Use
-rwith a directory when you need to search through nested folders. - Use
--includeor--excludeto narrow recursive searches by file name. - Use
-lwhen you need a list of files rather than matching lines.
For a very large tree, narrowing the search can save time and reduce noise. A broad recursive search may encounter binary files, unreadable files, or directories that are irrelevant to the question. Starting from a more specific folder is often simpler than trying to filter a huge result afterward.
Common Problems and How to Fix Them
No matches appear. Check spelling, capitalization, the directory, and the file pattern. If capitalization may vary, add -i. Also remember that a regular-expression character may not mean what it looks like; use -F for literal text.
The shell says a file cannot be found. Confirm the current directory and the exact path. A relative path is interpreted from the command’s current working directory. Quote paths containing spaces, and check whether a wildcard actually matches any files.
The output is too noisy. Narrow the directory, add an include pattern, or use -l to list matching file names only. If you need context around a match, options such as -A, -B, and -C can show lines after, before, or around it on common GNU grep installations; verify availability on the system you use.
Some files show binary-file messages or no readable lines. Grep is primarily intended for text. A file containing binary data may not be displayed like an ordinary text file. Do not assume that an empty-looking result proves the text is absent; confirm that the target file is a text file and that the command is searching the intended content.
There are permission errors. Grep can only read files permitted by the current account and environment. A recursive command may report files it cannot access. Search an accessible directory or ask the relevant administrator about appropriate access rather than treating those files as searched successfully.
How Grep Compares With Other Search Methods
Grep is a strong choice for quick searches in text files and for commands that can be repeated or incorporated into scripts. It prints matching lines directly and works naturally with shell wildcards and directory paths. It does not, by itself, provide a graphical interface or a full index of every document on a computer.
A file manager’s search may be easier when you prefer a visual interface or need to locate files by name. A code editor can be more helpful when you want search results linked to editable files or need project-aware features. A dedicated indexing tool may be better for repeatedly searching a very large collection, but it has different setup and freshness considerations. These approaches serve related needs; they are not interchangeable in every situation.
Frequently Asked Questions
How do I grep all files in a folder?
For files in the current folder, use a suitable wildcard, such as grep 'text' *, or a narrower one such as grep 'text' *.txt. To include nested directories, use grep -r 'text' folder/.
How do I show which file contains the text?
When searching multiple files, grep normally prefixes matching lines with their file names. Use -H to request names explicitly, or use -l to print only the names of files with matches.
How do I search without caring about uppercase or lowercase?
Add -i, as in grep -i 'failed' *.log. This can match variants such as FAILED and Failed.
How do I search for a phrase with punctuation?
Quote the pattern and use -F for a literal search, for example grep -F 'config.v2' settings.txt. This avoids interpreting punctuation as regular-expression syntax.
Does grep search inside subfolders automatically?
No. Searching a list of files or a wildcard usually covers only the paths supplied by the shell. Add -r and a directory path to search recursively.
A Practical Starting Point
For a quick search, begin with grep -n 'your text' file1 file2. Switch to -F when you mean literal text, add -i if capitalization is uncertain, and use -r when the files are spread through a directory tree. If results are unexpectedly broad or incomplete, check the paths, file types, permissions, and local grep documentation. Those checks usually reveal whether the issue is the pattern, the shell, or the set of files being searched.