0

when doing something like this:

cat *.* | grep mystring

is it possible to know from which file each match comes from? If not what would be the alternative?

3
  • 2
    Alternative: grep mystring *.*
    – Cyrus
    Dec 17, 2014 at 21:16
  • What do you mean by "something like"? For your example grep -H mystring *.* works perfectly adequately (-H in case *.* matches only one file).
    – AFH
    Dec 17, 2014 at 21:18
  • Note that cat has no option to display file names, so once you have entered cat *.* there can be no way to show file names.
    – AFH
    Dec 17, 2014 at 21:37

1 Answer 1

3

Use grep -l:

-l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed.

E.g.

$ echo foo > file1 $ echo bar > file2 $ grep -l foo * file1

Or with line numbers where the string occured:

$ grep -rn foo . file1:1:foo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .