I have a bunch of .html files in a directory. I want to look through each file and match a pattern (MD5). All of that is easy enough. The problem is I need to know what file the match was found in.

cat *.html | grep 75447A831E943724DD2DE9959E72EE31

Only returns the HTML page content where the match is found, but it doesn't tell me the file which it was found in. How can I get grep to show me the filename where my match is found?

  • The provided suggestion really doesn't work.... All I get is 'standard input' cat *.html | grep -i -H 1ee024007823cc0bfbefba98ba1e1f4c (standard input):<html><head><title> – BeMy Friend Feb 19 '15 at 7:02
  • 4
    you're missing the point. You don't need to use cat. The cat prevents grep from distinguishing individual files. – muru Feb 19 '15 at 7:17
  • You might found useful this Stack Overflow post. – Pablo Bianchi Aug 3 '17 at 0:26
grep -H 75447A831E943724DD2DE9959E72EE31 *.html

-H, --with-filename
              Print the file name for each match. This is
              the default when there is more than one file
              to search.
| improve this answer | |
  • 3
    As it says, that's the default for multiple files, so as long as there's more than one html file, -H isn't necessary. – muru Feb 19 '15 at 6:15
  • 1
    Sorry my question was offensive. I should have said I've been working on this for a while and I've used a number of switches AND have read the MAN page where is says it lists the filename by default.... But what I get is (standard input) where it should list the filename. – BeMy Friend Feb 19 '15 at 6:51
  • and that's only if I give the -H switch. If I do only what I listed in the OP, then it doesn't provide any filename. – BeMy Friend Feb 19 '15 at 6:53
  • 3
    @BeMyFriend, the problem is cat. cat means concatenate. If you concatenate all the files togheter before passing them to grep, grep see just one big standard input file and can't (short of doing divination) give you back any information about the single files. See?. Cyrus, what about explaining this in the answer? – Rmano Feb 19 '15 at 8:09
  • OK, the problem is cat command, but what can be a solution? – Josef Klimuk Jan 10 '18 at 5:35

I use this one all the time to look for files containing a string, RECURSIVELY in a directory (that means, traversing any sub sub sub folder) grep -Ril "yoursearchtermhere"

  • R is to search recursively (following symlinks)
  • i is to make it case insensitive
  • l is just to list the name of the files.

so answering your question grep -l '75447A831E943724DD2DE9959E72EE31' *.html will do but you can just do grep -Ril '75447A831E943724DD2DE9959E72EE31' to look for that string, case insensitive, in any file in any subfolder

| improve this answer | |
  • macOS requires a file def always, may it be a wildcard. Otherwise, it assumes input from stdin (grep: warning: recursive search of stdin, whatever recursive stdin input would mean :-). In other words: grep -Ril 'texttofind' * – Ville Jun 9 '18 at 23:47
  • grep -RiH 'pattern' shows files name and matched line – Ikrom Oct 12 '18 at 8:35

You can try this

grep -rl '75447A831E943724DD2DE9959E72EE31' * > found.txt
| improve this answer | |
grep -r -H 75447A831E943724DD2DE9959E72EE31 *.html | awk -F : ' { print $1 } '

Alternative to

grep -r -l 75447A831E943724DD2DE9959E72EE31 *.html

Doing above will search recursively in the folder and subfolders and print the path of the file...

| improve this answer | |
  • grep -r -l is the only thing worked to me. – Josef Klimuk Jan 10 '18 at 6:13

The answer posted by Cyrus is absolutely proper and is The Right WayTM to do it with grep if we only need to find files. When filenames need to additional parsing or operations on the matched filenames, we can resort to using while loop with if statement. Here's an example where list of filenames comes from very commonly used find+while structure for safe parsing of filenames.

find -type f -name "*.html" -print0 | while IFS= read -r -d '' filename
    if grep -q 'PATTERN' "$filename"
        printf "%s found in %s\n" 'PATTERN' "$filename"
        # Here we can insert another command or function
        # to perform other operations on the filename
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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