copied to clipboard!
string grep

Mastering the Grep Command to Show Filename Options: A Complete Guide

updated: 2026/06/21 created: 2026/06/21

Introduction

grep is a representative command for performing string searches in Linux and Unix-like environments.

Since it can show the file name along with the search results, it is useful for investigating multiple files and analyzing logs.

This article explains, in an easy-to-understand way, everything from the basics to practical applications of showing file names with grep.

Reference: GNU grep

Basic syntax for showing file names with grep

Creating the file

cat << 'EOF' > input.txt apple banana grape EOF

Command to run

grep -l "banana" input.txt

Output

input.txt

Command to run

grep -l "orange" input.txt

Output

No output

How it works

Item Description
grep Command that searches for a string within a file
-l Option that shows only the file name rather than the matching line
"banana" The string to search for
input.txt The file to search
Match found The file name is shown
No match Nothing is shown

Explanation

grep -l is used when you want to show only the names of files that contain the search string.
It is convenient for checking search results across multiple files.

How to show only the file name with grep

Creating the file

cat << 'EOF' > input.txt apple banana orange EOF

Command to run

grep -l "apple" input.txt

Output

input.txt

Command to run

grep -rl "apple" .

Output

./input.txt

How it works

Item Description
grep Command that searches for a string within a file
-l Shows only the file name rather than the matching line
-r Recursively searches through subdirectories
grep -l "apple" input.txt If "apple" exists in the specified file, only the file name is shown
grep -rl "apple" . Searches for "apple" from the current directory downward and shows only the matching file names

Explanation

Use the -l option to show only the file name with grep.
This is convenient when you want to list only the files that contain the search string.

How to show the file name and the matching line at the same time with grep

Creating the file

cat << 'EOF' > input.txt apple banana orange apple juice EOF

Command to run

grep -H "apple" input.txt

Output

input.txt:apple
input.txt:apple juice

Command to run

grep --with-filename "apple" input.txt

Output

input.txt:apple
input.txt:apple juice

How it works

Item Description
grep Searches for the specified string and shows the matching lines
-H Shows the file name at the start of each matching line
--with-filename Shows the file name, same as -H
input.txt:apple The line in input.txt that matched "apple"
Multi-file search Using something like grep -H "apple" *.txt makes it easy to identify which file the match came from

Explanation

Use the -H option when you want grep to show the file name along with the matching content. Since "filename:" is prepended to the result, you can easily check which file a matching line belongs to.

How to show file names when searching multiple files with grep

Creating the file

cat << 'EOF' > file1.txt apple banana orange EOF

Creating the file

cat << 'EOF' > file2.txt grape banana melon EOF

Creating the file

cat << 'EOF' > file3.txt peach apple kiwi EOF

Command to run

grep "banana" file1.txt file2.txt file3.txt

Output

file1.txt:banana
file2.txt:banana

Command to run

grep -H "apple" file1.txt file2.txt file3.txt

Output

file1.txt:apple
file3.txt:apple

Command to run

grep -nH "apple" file1.txt file2.txt file3.txt

Output

file1.txt:1:apple
file3.txt:2:apple

How it works

Item Description
grep "string" multiple-files When multiple files are specified, the file name is shown at the start of each matching line
-H Forces the file name to be shown
-n Shows the line number of the match
file1.txt:file content Output is in the format filename:matching line

Explanation

When you search multiple files with grep, the file name is shown by default at the start of each matching line.

Adding the -H option lets you force the file name to be shown even when searching a single file.

How to force grep to show the file name

Creating the file

cat << 'EOF' > input.txt apple banana orange EOF

Command to run

grep apple input.txt

Output

apple

Command to run

grep -H apple input.txt

Output

input.txt:apple

Command to run

grep apple input.txt /dev/null

Output

input.txt:apple

How it works

Method Example command File name shown How it works
Normal search grep apple input.txt No The file name is omitted because there is only one file to search
-H option grep -H apple input.txt Yes Forces the file name to be shown regardless of the number of files
Adding /dev/null grep apple input.txt /dev/null Yes Makes grep treat it as a multi-file search, so the file name is shown

Explanation

By default, grep does not show the file name when there is only one file to search.

If you want to force the file name to be shown, using the -H option is the clearest and recommended approach.

How to prevent grep from showing the file name

Creating the file

cat << 'EOF' > input.txt apple banana apple pie EOF

Command to run

grep "apple" input.txt

Output

apple
apple pie

Command to run

grep "apple" *.txt

Output

input.txt:apple
input.txt:apple pie

Command to run

grep -h "apple" *.txt

Output

apple
apple pie

How it works

Command Behavior
grep "apple" input.txt When only one file is specified, the file name is normally not shown
grep "apple" *.txt When searching multiple files, the file name is shown at the start of each matching line
grep -h "apple" *.txt Suppresses the file name and shows only the matching content
grep -H "apple" input.txt Forces the file name to be shown even for a single file

Explanation

When grep searches multiple files, it shows the file name so you can tell which file matched.

Using the -h option suppresses the file name show, showing only the matching lines.

How to show file names that did not match with grep

Creating the file

cat << 'EOF' > input.txt apple banana orange EOF

Command to run

grep -L "grape" input.txt

Output

input.txt

Command to run

grep -L "apple" input.txt

Output

No output

How it works

Command Description Result
grep -L "grape" input.txt Shows the name of the file that does not contain the specified string input.txt
grep -L "apple" input.txt The file name is not shown because the specified string is present No output
grep -l "apple" input.txt Shows the name of the file that matched the specified string input.txt

Explanation

grep -L shows only the names of files in which the search string was not found.

When working with multiple files, this lets you efficiently identify the files that did not match.

How to show file names while performing a recursive search with grep

Creating the file

cat << 'EOF' > input.txt grep sample text grep target text no match EOF

Creating the file

mkdir -p src/sub log

Creating the file

sed -n '1p' input.txt > src/main.txt

Creating the file

sed -n '2p' input.txt > src/sub/test.txt

Creating the file

sed -n '3p' input.txt > log/app.log

Command to run

grep -rn "grep" .

Output

./src/main.txt:1:grep sample text
./src/sub/test.txt:1:grep target text
./input.txt:1:grep sample text
./input.txt:2:grep target text

Command to run

grep -rl "grep" .

Output

./src/main.txt
./src/sub/test.txt
./input.txt

How it works

Item Description
grep Searches for the specified string
-r Recursively searches subdirectories as well
-n Shows the line number of the match
-l Shows only the names of matching files
"grep" The string being searched for
. Targets the current directory and everything below it

Explanation

Using grep -rn lets you check the matching file name, line number, and content all together.

If you only want the file names, using grep -rl is more efficient.

How to narrow down the target file names with grep

Creating the file

cat << 'EOF' > input.txt apple banana orange error: file not found warning: disk full sample.txt config.txt error.log report.txt EOF

Command to run

grep ".txt$" input.txt

Output

sample.txt
config.txt
report.txt

Command to run

grep -H ".txt$" input.txt

Output

input.txt:sample.txt
input.txt:config.txt
input.txt:report.txt

Command to run

grep -l "error" input.txt

Output

input.txt

How it works

Command How it works Use case
grep "\.txt$" input.txt Searches for and shows lines ending in .txt Extracting candidate file names
grep -H "\.txt$" input.txt Prepends the source file name to each search result Checking which file the match came from
grep -l "error" input.txt Shows only the target file name rather than the matching content Getting a list of matching file names

Explanation

With grep, using -H lets you show the file name together with the search results.
Also, using -l lets you show only the file names that meet the condition, rather than the matching content.

How to exclude specific file names from grep results

Creating the file

cat << 'EOF' > input.txt sample.log error.log access.log debug.log system.log EOF

Command to run

grep -v '^error.log$' input.txt

Output

sample.log
access.log
debug.log
system.log

Command to run

grep -v 'error.log' input.txt

Output

sample.log
access.log
debug.log
system.log

How it works

Item Description
grep Command that searches text and shows results
-v Excludes lines that match the condition
error.log Specifies the file name to exclude
^ Represents the start of the line, used for exact matching
$ Represents the end of the line, used for exact matching
input.txt The file being searched

Explanation

Using grep -v lets you exclude a specified file name from the shown results.

Combining ^ and $ allows you to exclude file names based on an exact match.

How to ignore case when showing file names with grep

Creating the file

cat << 'EOF' > input.txt Apple banana APPLE Orange apple EOF

Command to run

grep -i -H "apple" input.txt

Output

input.txt:Apple
input.txt:APPLE
input.txt:apple

How it works

Option Role
grep Searches for the specified string
-i Searches without distinguishing between uppercase and lowercase
-H Shows the file name at the start of each matching line
"apple" The target string being searched for
input.txt The file being searched

Explanation

Using grep -i lets you search for Apple, APPLE, and apple as the same string.

Adding -H also shows the file name alongside the matching content.

How to show file names that match a regular expression with grep

Creating the file

cat << 'EOF' > input.txt apple orange error: file not found banana error: permission denied grape EOF

Command to run

grep -l 'error:.*' input.txt

Output

input.txt

Command to run

grep -H 'error:.*' input.txt

Output

input.txt:error: file not found
input.txt:error: permission denied

How it works

Item Description
grep Searches text for lines matching a condition
'error:.*' A regular expression that starts with "error:" followed by any string
-l Shows only the name of the matching file, not the matching line
-H Prepends the file name to each matching line
input.txt The target file being searched

Explanation

Using grep -l lets you show only the names of files containing content that matches a regular expression.
This is convenient when you want to search multiple files and identify which ones match a given condition.

How to show file names that match multiple keywords with grep

Creating the file

cat << 'EOF' > input.txt grep show filename show filename with grep example of grep show want to output only the filename show the result of grep search EOF

Command to run

grep -El 'grep|show|filename' input.txt

Output

input.txt

Command to run

grep -l 'grep' input.txt | xargs grep -l 'show' | xargs grep -l 'filename'

Output

input.txt

How it works

Command Description
grep -E Uses extended regular expressions
grep|show|filename Matches any of the keywords (OR search)
-l Shows only the file name rather than the matching content
grep -l 'grep' Extracts files containing "grep"
xargs grep -l 'show' Narrows the extracted files down to those that also contain "show"
xargs grep -l 'filename' Finally extracts those that also contain "filename"

Explanation

Using grep -El lets you easily show the names of files matching any of several keywords.

If you want to find files that match all of the keywords, chain grep -l commands together with pipes to narrow down the results.

How to use grep together with the find command to show file names

Creating the file

cat << 'EOF' > input.txt error: database connection failed info: application started error: timeout occurred info: request completed EOF

Command to run

find . -type f -name "*.txt" -exec grep -H "error" {} \;

Output

./input.txt:error: database connection failed
./input.txt:error: timeout occurred

Command to run

find . -type f -name "*.txt" | xargs grep -H "error"

Output

./input.txt:error: database connection failed
./input.txt:error: timeout occurred

How it works

Element Description
find Searches for files that match the specified condition
-type f Targets only regular files
-name "*.txt" Searches for .txt files
grep Searches for the specified string
-H Shows the file name at the start of each search result
-exec grep -H "error" {} \; Runs grep on each file found by find
xargs grep -H "error" Passes the results from find to grep for searching

Explanation

Using grep -H lets you show the file name at the start of each search result.

Combining it with find lets you efficiently check matching strings and file names across multiple files.

How to extract only the shown file names from grep

Creating the file

cat << 'EOF' > input.txt apple orange banana apple juice EOF

Creating the file

cp input.txt input2.txt

Creating the file

echo "grape" > input3.txt

Command to run

grep "apple" input.txt

Output

apple
apple juice

Command to run

grep -l "apple" input.txt

Output

input.txt

Command to run

grep -l "apple" input*.txt

Output

input.txt
input2.txt

How it works

Option Description Output
grep "apple" input.txt Shows lines matching the pattern Matching lines
grep -l "apple" input.txt Shows only the matching file name File name
grep -l "apple" input*.txt Searches multiple files List of matching file names

Explanation

The -l in grep -l stands for "files with matches," and it outputs only the names of matching files instead of the matching lines.

This is convenient when searching multiple files and you want to identify the ones that meet a particular condition.

Causes and solutions when grep does not show the file name

Creating the file

cat << 'EOF' > input.txt apple banana orange EOF

Command to run

grep apple input.txt

Output

apple

Command to run

grep apple *.txt

Output

input.txt:apple

Command to run

grep -H apple input.txt

Output

input.txt:apple

Command to run

grep -h apple *.txt

Output

apple

How it works

Command File name shown Description
grep apple input.txt No The file name is omitted because there is only one file to search
grep apple *.txt Yes Treated as a multi-file search, so the file name is shown
grep -H apple input.txt Yes The -H option always shows the file name
grep -h apple *.txt No The -h option suppresses the file name show

Explanation

By default, grep does not show the file name when there is only one file to search.

If you always want the file name shown, run grep with the -H option.

Summary: how to show file names with grep

By combining options, grep gives you flexible control over how file names are shown.

Once you understand the basic syntax, making use of recursive search, regular expressions, and integration with find will greatly improve your search efficiency.

Choose the right options for your use case, and put grep to practical use in increasingly effective ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

©︎ 2025-2026 running terminal commands