Glam Prestige Journal

Bright entertainment trends with youth appeal.

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?

3

6 Answers

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.
0

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 questiongrep -l '75447A831E943724DD2DE9959E72EE31' *.html will do but you can just dogrep -Ril '75447A831E943724DD2DE9959E72EE31' to look for that string, case insensitive, in any file in any subfolder

2

You can try this

grep -rl '75447A831E943724DD2DE9959E72EE31' * > found.txt
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...

0

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
do if grep -q 'PATTERN' "$filename" then printf "%s found in %s\n" 'PATTERN' "$filename" # Here we can insert another command or function # to perform other operations on the filename fi
done
1

I would do it like this:

find . -type f -name=*.html -exec grep -H 75447A831E943724DD2DE9959E72EE31 {} \;

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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