340

How can I get grep to display the filename before the matching lines in its output?

0

6 Answers 6

504

Try this little trick to coax grep into thinking it is dealing with multiple files, so that it displays the filename:

grep 'pattern' file /dev/null

To also get the line number:

grep -n 'pattern' file /dev/null
9
  • 9
    Yes, it worked. Can you please tell me whats the significance of adding /dev/null in this command?
    – Vivek
    Mar 18, 2013 at 8:38
  • 63
    If grep is supplied with multiple file names, it will automatically display the file names before the match, but it will leave the file name out in case of a single input file. By using /dev/null as an extra input file grep "thinks" it dealing with multiple files, but /dev/null is of course empty, so it will not show up in the match list.. Mar 18, 2013 at 8:44
  • 35
    using /dev/null is a clever trick, but I think the suggestion below of the -H flag is a better answer.
    – JohnQ
    Nov 12, 2013 at 15:33
  • 25
    OK, but the -H option for grep is not part of POSIX, and the OS used is Solaris 10, where neither the standard grep, nor the POSIX compliant version of grep have the -H option. Nov 16, 2013 at 8:03
  • 5
    That's important if you are using grep with find, like find . -name foo -exec grep pattern {} \; - assuming plenty of files named foo in your subdirectory, find still hands grep a single file to operate on at a time. The /dev/null trick seems to work in all linux/osx flavors that I've tried, so kudos! Note I think that -H is cleaner, but as mentioned below, it might not be supported with your version of grep.
    – Jon V
    Feb 12, 2018 at 22:01
358

If you have the options -H and -n available (man grep is your friend):

$ cat file
foo
bar
foobar

$ grep -H foo file
file:foo
file:foobar

$ grep -Hn foo file
file:1:foo
file:3:foobar

Options:

-H, --with-filename

Print the file name for each match. This is the default when there is more than one file to search.

-n, --line-number

Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)

-H is a GNU extension, but -n is specified by POSIX

4
  • 3
    No, -H is not supported here.
    – Vivek
    Mar 18, 2013 at 8:39
  • 1
    Supported but it always gives grep: file: No such file or directory
    – pal4life
    Mar 10, 2014 at 17:15
  • 2
    works on ubuntu -H and -n. Probably with all newer versions of *nix
    – tgkprog
    May 22, 2014 at 9:51
  • 1
    I have struggled with that for years without evening thinking there were these options! Feb 18, 2015 at 23:08
58

No trick necessary.

grep --with-filename 'pattern' file

With line numbers:

grep -n --with-filename 'pattern' file
4
  • 3
    @codeforester I don't know of any other greps, so I don't know.
    – MD XF
    Feb 25, 2017 at 4:05
  • 2
    Cool ! this worked for linux. if you use mac, can install coreutils with brew. And remember add PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" to yours .bashrc
    – zw963
    Mar 9, 2017 at 10:54
  • 2
    This (also) worked in MinGW (1.0.17, I think). Apr 11, 2017 at 23:04
  • It works for me with the built-in Mac grep (2.5.1-FreeBSD). But to use the alternative grep from Homebrew, another way is to run brew install grep and then just use ggrep. No need to modify PATH.
    – wonderlr
    Jun 4, 2019 at 16:11
7

How about this, which I managed to achieve thanks, in part, to this post.

You want to find several files, lets say logs with different names but a pattern (e.g. filename=logfile.DATE), inside several directories with a pattern (e.g. /logsapp1, /logsapp2). Each file has a pattern you want to grep (e.g. "init time"), and you want to have the "init time" of each file, but knowing which file it belongs to.

find ./logsapp* -name logfile* | xargs -I{} grep "init time" {} \dev\null | tee outputfilename.txt

Then the outputfilename.txt would be something like

./logsapp1/logfile.22102015: init time: 10ms
./logsapp1/logfile.21102015: init time: 15ms
./logsapp2/logfile.21102015: init time: 17ms
./logsapp2/logfile.22102015: init time: 11ms

In general

find ./path_pattern/to_files* -name filename_pattern* | xargs -I{} grep "grep_pattern" {} \dev\null | tee outfilename.txt

Explanation:

find command will search the filenames based in the pattern

then, pipe xargs -I{} will redirect the find output to the {}

which will be the input for grep ""pattern" {}

Then the trick to make grep display the filenames \dev\null

and finally, write the output in file with tee outputfile.txt

This worked for me in grep version 9.0.5 build 1989.

1
  • 4
    Or just using find find ./path_pattern/to_files -type f -name "files*.log" -exec grep -Hn pattern {} \;
    – Martin
    Aug 31, 2016 at 13:42
1

This is a slight modification from a previous solution. My example looks for stderr redirection in bash scripts: grep '2>' $(find . -name "*.bash")

-7
grep 'search this' *.txt

worked for me to search through all .txt files (enter your own search value, of course).

Not the answer you're looking for? Browse other questions tagged or ask your own question.