Amazon Cyber Monday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux grep: Show Only One Match per File

Yes. Use grep -l (or grep --files-with-matches) to print each file name once when it contains at least one match. If you instead want one matching line per file, use grep -m 1; these options solve different problems.

What “one time for each file” means

The right command depends on what you want to see. By default, grep prints every matching line. A file with several matching lines can therefore appear several times when grep prints the file name along with each result. There are two common ways to reduce that output:

  • List each matching file once: use -l. This outputs file names, not the matching lines.
  • Show only the first matching line per file: use -m 1. This retains a matching line and, when searching multiple files, its file name.

Neither option means “print the first occurrence of the pattern within every line.” Grep normally reports matching lines, not individual occurrences, and the distinction matters when choosing flags.

List each matching file once with -l

For example, search all .log files in the current directory and list the files containing “timeout”:

grep -l 'timeout' ./*.log

Even if a log contains “timeout” on many lines, its name is printed once. Files without a match are omitted. To search a directory tree recursively, add -r:

grep -rl 'timeout' ./logs

Use -R instead when you also want grep to follow symbolic links encountered during the search. The exact handling of links and special files can depend on the option and the file layout, so use -r for the usual recursive search that does not follow symlinks by default.

The long form, --files-with-matches, is equivalent and can make scripts easier to read. Its counterpart, -L or --files-without-match, lists files that do not contain a match.

Keep one matching line with -m 1

If you want an example line rather than just a file name, use:

grep -m 1 'timeout' ./*.log

When grep searches multiple files, it normally prefixes each matching line with the file name. The -m NUM option limits the number of selected matching lines per input file; with -m 1, it stops selecting matches from that file after the first. This is not a limit of one line across the entire command: each input file can contribute its own first match.

For a recursive search that shows one matching line per file, a typical command is:

grep -rm 1 'timeout' ./logs

This is useful when you need a quick sample of what matched in each file. It does not guarantee a specific ordering of files, and the “first” line means the first match encountered while reading that file, not the newest, oldest, or most important match.

Choose options for the output you need

Grep’s options can be combined, but it helps to decide whether the output should be file names, matching text, or context around matches.

  • grep -l PATTERN FILES prints each matching file name once.
  • grep -m 1 PATTERN FILES prints at most one matching line from each file.
  • grep -n PATTERN FILES adds line numbers; combine with -m 1 to show the first matching line number per file.
  • grep -H PATTERN FILES forces file names in output, including when only one file is searched. This can be combined with -m 1.
  • grep -h PATTERN FILES suppresses file-name prefixes when searching multiple files.

For instance, to show the first matching line and its line number in each log:

grep -n -m 1 'timeout' ./*.log

Grep’s output format changes when it is given one input file versus multiple files. If a script must always include file names, -H makes that intent explicit. If it must print names only, -l is usually simpler than trying to suppress duplicate matching lines after the fact.

Recursive searches, filenames, and scripts

Recursive searches are often used to find which source files contain a setting or function name:

grep -rl 'DEBUG_MODE' ./src

For a fixed string rather than a regular expression, add -F, as in grep -rlF 'DEBUG_MODE' ./src. This treats pattern characters literally and can avoid confusion if the search text contains characters that have special meaning in regular expressions.

When filenames may contain spaces, grep’s ordinary line-oriented output can be awkward for later processing. GNU grep offers -Z (also spelled --null) to separate file names with a NUL byte. For example, grep -rlZ 'timeout' ./logs can be passed to tools that understand NUL-delimited names. Do not assume newline-separated file names are safe for every script: Unix filenames can themselves contain newlines.

Also keep grep’s exit status in mind. It returns status 0 when it finds a match, 1 when it finds no match, and a status greater than 1 for an error. In a shell script, “no matching files” may therefore affect a conditional or a pipeline even though the search ran successfully.

Common misconceptions and alternatives

A common mistake is to use -c expecting one result per file. The -c option prints a count of matching lines for each file; a count can be greater than one. Use -l if the question is simply whether a file has any match.

Another misconception is that -m 1 limits grep to one result total. It limits matching lines per input file. With one file, that is one line overall; with several files, each can produce one.

For one line total across all files, you need a different approach, and the desired meaning must be clear: the first result in argument order, or the first result in some sorted order? A simple pipeline such as grep 'timeout' ./*.log | head -n 1 takes the first output line, but it does not guarantee a particular file ordering unless the inputs are ordered deliberately. It also discards the other files’ results. Do not use that pipeline as a substitute for “one per file.”

Other tools can help when you need structured filtering or complex reporting, but standard grep already covers the usual cases: -l for file names and -m 1 for one matching line per file.

Related

Frequently Asked Questions

How do I make grep print a filename only once?
Use grep -l 'pattern' files. It prints each file that contains a match once, regardless of how many matching lines it has.

How do I show the first match in every file?
Use grep -m 1 'pattern' files. It prints at most the first matching line from each input file.

Does grep -m 1 mean one match total?
No. It sets the limit per input file. A command searching five files can print up to five matching lines.

How can I search subdirectories and list matching files?
Use grep -rl 'pattern' directory. The -r option searches recursively, and -l prints matching file names only.

What is the difference between grep -l and grep -c?
-l lists files with at least one match. -c prints the number of matching lines in each file, which may be more than one.

Linux grep: Show Only One Match per File
Scroll to top