Introduction
grep is a representative command for performing string searches on Linux and macOS.
It is especially useful because it can search across multiple files at once, making it valuable for log analysis, source code investigation, and many other situations.
This article explains, in an easy-to-understand way, everything from the basic syntax for searching multiple files with grep, how to specify search targets, wildcard searches, display settings for search results, condition specification, recursive search, and how to handle errors.
Reference: GNU grep
Basic syntax for searching multiple files with grep
Create file
cat << 'EOF' > file1.txt
apple
banana
orange
EOF
Create file
cat << 'EOF' > file2.txt
grape
banana
melon
EOF
Create file
cat << 'EOF' > file3.txt
apple
peach
melon
EOF
Command to run
grep "banana" file1.txt file2.txt file3.txt
Result
file1.txt:banana
file2.txt:banana
Command to run
grep "apple" file*.txt
Result
file1.txt:apple
file3.txt:apple
Command to run
grep -n "melon" file1.txt file2.txt file3.txt
Result
file2.txt:3:melon
file3.txt:3:melon
How it works
| Element | Description |
|---|---|
| grep | Text search command |
| "banana" | String to search for |
| file1.txt file2.txt file3.txt | Multiple target files to search |
| file*.txt | Specifying multiple files using a wildcard |
| -n | Displays the matching line number |
| filename:content | When searching multiple files, the matching filename is also displayed |
Explanation
grep can search multiple files at the same time by specifying the target files separated by spaces.
Using a wildcard (*) lets you search a large number of files all at once.
How to specify search targets for multiple files with grep
Create file
cat << 'EOF' > sample1.txt
Linux
grep command
error log
EOF
Create file
cat << 'EOF' > sample2.txt
Windows
grep multiple files
warning log
EOF
Create file
cat << 'EOF' > sample3.txt
MacOS
grep test
info log
EOF
Command to run
grep "grep" sample1.txt sample2.txt sample3.txt
Result
sample1.txt:grep command
sample2.txt:grep multiple files
sample3.txt:grep test
Command to run
grep "log" sample*.txt
Result
sample1.txt:error log
sample2.txt:warning log
sample3.txt:info log
Command to run
grep -n "grep" sample1.txt sample2.txt sample3.txt
Result
sample1.txt:2:grep command
sample2.txt:2:grep multiple files
sample3.txt:2:grep test
How it works
| Item | Description |
|---|---|
| Specifying search targets | Specify multiple filenames separated by spaces |
| Specifying with a wildcard | Multiple files can be specified at once, such as sample*.txt |
| Output format | The filename is displayed at the beginning of each matching line |
| -n option | Also displays the line number of the match |
| Processing flow | grep reads and searches each specified file in order |
Explanation
grep can search multiple files at the same time and shows which file the match was found in.
When dealing with a large number of files, using a wildcard makes the search more efficient.
Searching multiple files with grep using wildcard specification
Create file
cat << 'EOF' > app.log
INFO: Application started
ERROR: Database connection failed
INFO: Retry processing
EOF
Create file
cat << 'EOF' > system.log
INFO: System boot
ERROR: Disk space low
INFO: Cleanup completed
EOF
Create file
cat << 'EOF' > web.log
INFO: Request received
ERROR: Internal Server Error
INFO: Response sent
EOF
Command to run
grep "ERROR" *.log
Result
app.log:ERROR: Database connection failed
system.log:ERROR: Disk space low
web.log:ERROR: Internal Server Error
Command to run
grep -n "INFO" *.log
Result
app.log:1:INFO: Application started
app.log:3:INFO: Retry processing
system.log:1:INFO: System boot
system.log:3:INFO: Cleanup completed
web.log:1:INFO: Request received
web.log:3:INFO: Response sent
Command to run
grep -l "ERROR" *.log
Result
app.log
system.log
web.log
How it works
| Element | Description |
|---|---|
| grep | Command that searches for strings inside files |
| "ERROR" | String to search for |
| *.log | Specifies multiple files ending in .log via wildcard |
| -n | Displays results with line numbers |
| -l | Displays only the matching filenames |
| Search process | Scans through all specified files in order and outputs matching lines |
Explanation
Using a wildcard (*.log) with grep allows you to search multiple files at once. This is a convenient method commonly used for log analysis and configuration file investigation.
Displaying filenames in grep search results for multiple files
Create file
cat << 'EOF' > file1.txt
apple
banana
orange
EOF
Create file
cat << 'EOF' > file2.txt
grape
banana
melon
EOF
Create file
cat << 'EOF' > file3.txt
apple
peach
melon
EOF
Command to run
grep "banana" file1.txt file2.txt file3.txt
Result
file1.txt:banana
file2.txt:banana
Command to run
grep -H "apple" file1.txt file2.txt file3.txt
Result
file1.txt:apple
file3.txt:apple
Command to run
grep -nH "melon" file1.txt file2.txt file3.txt
Result
file2.txt:3:melon
file3.txt:3:melon
How it works
| Item | Description |
|---|---|
| grep | Command that searches for a specified string |
| Specifying multiple files | Listing multiple filenames lets you search them all together |
| Filename display | When multiple files are targeted, the filename is automatically shown at the start of the search result |
| -H | Always displays the filename |
| -n | Displays the matching line number |
| Output format | filename:content or filename:line number:content |
Explanation
When you search multiple files with grep, the filename is automatically displayed so you can tell which file the match came from.
If you want the filename displayed even for a single file, use the -H option.
Displaying line numbers in grep search results for multiple files
Create file
cat << 'EOF' > file1.txt
apple
banana
orange
grape
EOF
Create file
cat << 'EOF' > file2.txt
melon
banana
peach
apple
EOF
Create file
cat << 'EOF' > file3.txt
kiwi
lemon
banana
EOF
Command to run
grep -n "banana" file1.txt file2.txt file3.txt
Result
file1.txt:2:banana
file2.txt:2:banana
file3.txt:3:banana
Command to run
grep -Hn "apple" file1.txt file2.txt file3.txt
Result
file1.txt:1:apple
file2.txt:4:apple
How it works
| Item | Description |
|---|---|
| grep | Command that searches text |
| Specifying multiple files | List them like file1.txt file2.txt file3.txt |
| -n | Displays the line number of the matching line |
| -H | Always displays the filename |
| Output format | filename:line number:matching line |
Explanation
Using grep -n lets you check the line numbers of matches when searching multiple files.
Combining it with -H also shows the target filename, making it easier to identify the results.
Searching multiple files with grep without case sensitivity
Create file
cat << 'EOF' > file1.txt
Linux
Ubuntu
GREP
EOF
Create file
cat << 'EOF' > file2.txt
linux server
CentOS
grep command
EOF
Command to run
grep -i "grep" file1.txt file2.txt
Result
file1.txt:GREP
file2.txt:grep command
Command to run
grep -i "linux" file1.txt file2.txt
Result
file1.txt:Linux
file2.txt:linux server
Command to run
grep -i "grep\|linux" file1.txt file2.txt
Result
file1.txt:Linux
file1.txt:GREP
file2.txt:linux server
file2.txt:grep command
How it works
| Element | Description |
|---|---|
| grep | Command that searches text |
| -i | Searches without distinguishing between uppercase and lowercase |
| file1.txt file2.txt | Searches multiple files at the same time |
| grep\|linux | Searches for lines matching grep or linux |
| filename:content | When searching multiple files, the matching filename is also shown |
Explanation
The grep command can take multiple target files, and adding the -i option lets you search without distinguishing uppercase from lowercase.
Since multiple files can be searched at once, this is efficient for log investigation and checking configuration files.
Searching for multiple keywords across multiple files with grep
Create file
cat << 'EOF' > file1.txt
grep is a text search command
You can search across multiple files
EOF
Create file
cat << 'EOF' > file2.txt
grep can search multiple keywords
Extract strings from files
EOF
Create file
cat << 'EOF' > file3.txt
grep is useful for log file analysis
EOF
Command to run
grep -E 'grep|multiple|file' file1.txt file2.txt file3.txt
Result
file1.txt:grep is a text search command
file1.txt:You can search across multiple files
file2.txt:grep can search multiple keywords
file2.txt:Extract strings from files
file3.txt:grep is useful for log file analysis
Command to run
grep -E 'grep|multiple' file*.txt
Result
file1.txt:grep is a text search command
file1.txt:You can search across multiple files
file2.txt:grep can search multiple keywords
file3.txt:grep is useful for log file analysis
How it works
| Element | Description |
|---|---|
| grep | Command that performs text searches |
| -E | Enables extended regular expressions |
| grep|multiple|file | | performs an OR search across multiple keywords |
| file1.txt file2.txt file3.txt | Multiple target files to search |
| file*.txt | Specifying multiple files using a wildcard |
Explanation
Using grep's -E option lets you search for multiple keywords together with an OR condition.
When multiple files are specified at once, the matching lines and their filenames are displayed.
Using regular expressions to search multiple files with grep
Create file
cat << 'EOF' > file1.txt
Linux
grep
sed
EOF
Create file
cat << 'EOF' > file2.txt
awk
grep
find
EOF
Create file
cat << 'EOF' > file3.txt
docker
kubernetes
grep
EOF
Command to run
grep -E 'grep|find' file1.txt file2.txt file3.txt
Result
file1.txt:grep
file2.txt:grep
file2.txt:find
file3.txt:grep
Command to run
grep -E '^grep$' file1.txt file2.txt file3.txt
Result
file1.txt:grep
file2.txt:grep
file3.txt:grep
Command to run
grep -En 'grep|find' file1.txt file2.txt file3.txt
Result
file1.txt:2:grep
file2.txt:2:grep
file2.txt:3:find
file3.txt:3:grep
How it works
| Item | Description |
|---|---|
| grep | Command that searches for strings inside files |
| -E | Uses extended regular expressions |
| -n | Displays the matching line number |
| grep|find | Matches either grep or find |
| ^grep$ | Matches only when the entire line is exactly "grep" |
| Specifying multiple files | Can specify multiple files like file1.txt file2.txt file3.txt |
| Filename display | When searching multiple files, the matching filename is also shown |
Explanation
grep can search multiple files at once, and using -E lets you write OR conditions and other regular expressions concisely.
Since results are shown with the filename, you can immediately tell which file the match was found in.
Searching for an exact match string across multiple files with grep
Create file
cat << 'EOF' > file1.txt
apple
orange
banana
EOF
Create file
cat << 'EOF' > file2.txt
grape
apple
melon
EOF
Create file
cat << 'EOF' > file3.txt
pineapple
apple
Apple
EOF
Command to run
grep -x "apple" file1.txt file2.txt file3.txt
Result
file1.txt:apple
file2.txt:apple
file3.txt:apple
Command to run
grep -x -n "apple" file*.txt
Result
file1.txt:1:apple
file2.txt:2:apple
file3.txt:2:apple
How it works
| Item | Description |
|---|---|
| grep | Command that searches text |
| Specifying multiple files | List them like file1.txt file2.txt file3.txt |
| -x | Only displays a line when the entire line exactly matches the search string |
| -n | Displays the matching line number |
| Output format | filename:content or filename:line number:content |
Explanation
Searching multiple files at the same time with grep lets you check all at once which files contain the matching string.
Using the -x option restricts the search to exact matches rather than partial matches.
Searching multiple files for an AND condition with grep
Create file
cat << 'EOF' > file1.txt
server=web01
status=active
env=prod
EOF
Create file
cat << 'EOF' > file2.txt
server=web02
status=inactive
env=prod
EOF
Create file
cat << 'EOF' > file3.txt
server=web03
status=active
env=dev
EOF
Command to run
grep -l "status=active" file*.txt | xargs grep -l "env=prod"
Result
file1.txt
Command to run
grep -l "status=active" file*.txt | xargs grep "env=prod"
Result
file1.txt:env=prod
How it works
| Item | Description |
|---|---|
| First grep | Extracts filenames containing status=active from multiple files |
| -l option | Displays only the matching filenames |
| xargs | Passes the result of the first grep as arguments to the next grep |
| Second grep | Searches the narrowed-down files for env=prod |
| Achieving the AND condition | Narrows down step by step using the first and second grep |
Explanation
Since grep alone has no AND condition option, this is achieved by combining grep -l with xargs.
This is convenient when you want to identify target files across multiple files using multiple conditions.
Searching multiple files for a NOT condition with grep
Create file
cat << 'EOF' > file1.txt
apple
orange
banana
EOF
Create file
cat << 'EOF' > file2.txt
grape
orange
melon
EOF
Create file
cat << 'EOF' > file3.txt
apple
kiwi
peach
EOF
Command to run
grep -L "orange" file1.txt file2.txt file3.txt
Result
file3.txt
Command to run
grep -v "orange" file1.txt file2.txt
Result
file1.txt:apple
file1.txt:banana
file2.txt:grape
file2.txt:melon
Command to run
grep -L "apple" file*.txt
Result
file2.txt
How it works
| Option | Mechanism | Use case |
|---|---|---|
| -L | Displays only the names of files that do not contain the specified string | Finding files with a NOT condition across multiple files |
| -v | Excludes matching lines from the output | Extracting lines using a NOT condition |
| file1.txt file2.txt file3.txt | Searches multiple files at the same time | grep search across multiple files |
Explanation
With grep, using -L lets you easily identify "files that do not contain a string."
Also, using -v lets you extract only the lines that exclude a specific string from multiple files.
Recursively searching multiple files with grep
Create file
mkdir -p sample/app sample/log
Create file
cat << 'EOF' > sample/app/main.txt
database_host=localhost
api_endpoint=https://example.com
EOF
Create file
cat << 'EOF' > sample/app/config.txt
database_user=admin
database_password=secret
EOF
Create file
cat << 'EOF' > sample/log/app.log
INFO: application started
ERROR: database connection failed
EOF
Command to run
grep -r "database" sample
Result
sample/app/config.txt:database_user=admin
sample/app/config.txt:database_password=secret
sample/app/main.txt:database_host=localhost
sample/log/app.log:ERROR: database connection failed
Command to run
grep -rn "database" sample
Result
sample/app/config.txt:1:database_user=admin
sample/app/config.txt:2:database_password=secret
sample/app/main.txt:1:database_host=localhost
sample/log/app.log:2:ERROR: database connection failed
Command to run
grep -rl "database" sample
Result
sample/app/config.txt
sample/app/main.txt
sample/log/app.log
How it works
| Option | Description |
|---|---|
| grep | Command that searches for strings |
| -r | Searches recursively through a directory's contents |
| -n | Displays the matching line number |
| -l | Displays only the matching filenames |
| "database" | Search keyword |
| sample | Target directory to search |
Explanation
Using grep -r lets you recursively search through a directory containing multiple files.
Combining it with -n or -l lets you efficiently check just the line numbers or just the target files.
Excluding files when searching multiple files with grep
Create file
cat << 'EOT' > file1.txt
error: connection failed
info: process started
EOT
Create file
cat << 'EOT' > file2.txt
warning: disk usage high
error: timeout occurred
EOT
Create file
cat << 'EOT' > ignore.log
error: test data
EOT
Command to run
grep "error" file*.txt --exclude=file2.txt
Result
file1.txt:error: connection failed
Command to run
grep -r "error" . --include="*.txt" --exclude="file2.txt"
Result
./file1.txt:error: connection failed
How it works
| Item | Description |
|---|---|
| grep | Searches for a specified string |
| file*.txt | Targets multiple files together for the search |
| --exclude=file2.txt | Excludes the specified file from the search target |
| -r | Searches recursively through a directory's contents |
| --include="*.txt" | Restricts the file type that is targeted by the search |
Explanation
When searching multiple files, grep lets you exclude unwanted files from the search target by using --exclude.
This is often used when you want to exclude log files or temporary files.
Combining grep and find to search multiple files
Create file
cat << 'EOF' > file1.txt
Linux
grep command
find command
EOF
Create file
cat << 'EOF' > file2.txt
grep multiple files
search target
EOF
Create file
cat << 'EOF' > file3.txt
sample text
grep test
EOF
Command to run
find . -type f -name "*.txt" -exec grep -H "grep" {} \;
Result
./file2.txt:grep multiple files
./file3.txt:grep test
./file1.txt:grep command
Command to run
find . -type f -name "*.txt" | xargs grep -H "grep"
Result
./file2.txt:grep multiple files
./file3.txt:grep test
./file1.txt:grep command
How it works
| Command element | Role |
|---|---|
| find . | Searches under the current directory |
| -type f | Targets only regular files |
| -name "*.txt" | Targets .txt files for the search |
| -exec grep -H "grep" {} \; | Runs grep against each file that was found |
| xargs grep -H "grep" | Passes find's results to grep all at once |
| grep -H | Displays the matching line and its filename |
Explanation
By narrowing down target files with find and searching their content with grep, you can efficiently search multiple files.
When handling a large number of files, using xargs improves execution efficiency.
Saving grep search results from multiple files to another file
Create file
cat << 'DATA' > file1.txt
error: database connection failed
info: application started
DATA
Create file
cat << 'DATA' > file2.txt
warning: disk usage high
error: memory allocation failed
DATA
Create file
cat << 'DATA' > file3.txt
info: backup completed
error: network timeout
DATA
Command to run
grep "error" file1.txt file2.txt file3.txt > result.txt
Command to run
cat result.txt
Result
file1.txt:error: database connection failed
file2.txt:error: memory allocation failed
file3.txt:error: network timeout
How it works
| Element | Description |
|---|---|
| grep | Searches for a specified string |
| "error" | Target search keyword |
| file1.txt file2.txt file3.txt | Multiple target files to search |
| > result.txt | Redirect that saves the search results to a file |
| cat result.txt | Displays the saved content |
Explanation
grep can search multiple files at the same time, and you can save the search results to a separate file using >.
This is commonly used when extracting and managing a specific string from a large number of log files.
Common errors and solutions when searching multiple files with grep
Create file
cat << 'EOT' > file1.txt
error: database connection failed
info: application started
EOT
Create file
cat << 'EOT' > file2.txt
warning: disk usage 80%
error: timeout occurred
EOT
Create file
cat << 'EOT' > file3.txt
info: backup completed
EOT
Command to run
grep "error" file1.txt file2.txt file3.txt
Result
file1.txt:error: database connection failed
file2.txt:error: timeout occurred
Command to run
grep "error" file1.txt file2.txt file4.txt
Result
file1.txt:error: database connection failed
file2.txt:error: timeout occurred
grep: file4.txt: No such file or directory
Command to run
grep "error" *.log
Result
no output
Command to run
grep "error" file1.txt file2.txt file4.txt 2>/dev/null
Result
file1.txt:error: database connection failed
file2.txt:error: timeout occurred
How it works
| Situation | Cause | Solution |
|---|---|---|
| No such file or directory | The specified file does not exist | Check the filename |
| grep: *.log: No such file or directory | No files match the wildcard | Use ls to check the target files |
| No results are displayed | The search string does not exist | Review the search condition |
| Many errors are displayed | A nonexistent file is included | Suppress standard error output with 2>/dev/null |
Explanation
When searching multiple files with grep, errors can occur due to nonexistent files or failed wildcard expansion.
Checking for the file's existence beforehand, and using 2>/dev/null when necessary, allows for more efficient searching.
Key points for searching multiple files with grep
When searching multiple files with grep, it is important to first understand the basic syntax.
Building on that, making use of wildcard target specification and the options for displaying filenames and line numbers improves work efficiency and allows you to use grep in a more practical way.

