1846733 Members
3636 Online
110256 Solutions
New Discussion

Re: command grep/find

 
SOLVED
Go to solution
Jairo Campana
Trusted Contributor

command grep/find

hello , I have 500 directories and need obtaing the name of file ,that it contains the word domain.com
I excute and obtaing ,but not the file name:
find . -name *.htm -exec grep domain.com {} \;

action=http://domain.com
legionx
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: command grep/find

Your wildcard needs to be enclosed in quotes or you will get unexpected problems if *.htm exists in your current working directory.

find . -name '*.htm' | while read X
do
grep -q -i "domain.com" ${X}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "${X}"
fi
done
If it ain't broke, I can fix that.
Rick Garland
Honored Contributor
Solution

Re: command grep/find

Use 'grep -l' in your command

find . -name "*.htm" -exec grep -l domain.com {} \;

The -l option tells grep to list the file names and not the contents of the file.
Dave Hutton
Honored Contributor

Re: command grep/find

Try this way from the man page of grep.
find . -name *.html | xargs grep domain.com

That should list the filename first.
Robert Salter
Respected Contributor

Re: command grep/find

Try;

DOM=`find . -name *.htm`;grep "domain.com" $DOM

it will give you the file names
Time to smoke and joke
Amit Agarwal_1
Trusted Contributor

Re: command grep/find

You can make grep print file name with every matching line, by passing /dev/null as second file.

$ find . -name ".html" -exec grep 'domain.com' {} /dev/null \;