How can I get grep
to display the filename before the matching lines in its output?
6 Answers
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
-
9Yes, it worked. Can you please tell me whats the significance of adding
/dev/null
in this command?– VivekMar 18, 2013 at 8:38 -
63If
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 filegrep
"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 -
35using /dev/null is a clever trick, but I think the suggestion below of the -H flag is a better answer.– JohnQNov 12, 2013 at 15:33
-
25OK, 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 -
5That's important if you are using grep with find, like
find . -name foo -exec grep pattern {} \;
- assuming plenty of files namedfoo
in your subdirectory,find
still handsgrep
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 ofgrep
.– Jon VFeb 12, 2018 at 22:01
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
-
3
-
1Supported but it always gives grep: file: No such file or directory– pal4lifeMar 10, 2014 at 17:15
-
2
-
1I have struggled with that for years without evening thinking there were these options! Feb 18, 2015 at 23:08
No trick necessary.
grep --with-filename 'pattern' file
With line numbers:
grep -n --with-filename 'pattern' file
-
3
-
2Cool ! 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– zw963Mar 9, 2017 at 10:54
-
2
-
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 useggrep
. No need to modify PATH.– wonderlrJun 4, 2019 at 16:11
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.
-
4Or just using find
find ./path_pattern/to_files -type f -name "files*.log" -exec grep -Hn pattern {} \;
– MartinAug 31, 2016 at 13:42
This is a slight modification from a previous solution. My example looks for stderr redirection in bash scripts:
grep '2>' $(find . -name "*.bash")
grep 'search this' *.txt
worked for me to search through all .txt files (enter your own search value, of course).