1752688 Members
5244 Online
108789 Solutions
New Discussion юеВ

Re: find a string

 
SOLVED
Go to solution
peterchu
Super Advisor

find a string

I want to find the string "abc" in the path /etc like below script , but the reason only show the content of the file but not the file name , how can I know which file(s) contains the string ? thx.

#cd /etc
#find . -type f -exec grep -i "abc" {} \;
12 REPLIES 12
Chris Wilshaw
Honored Contributor
Solution

Re: find a string

Try

find . -type f -exec grep -il "abc" {} \;

or, if you're on HP-UX 11.*,

find . -type f -exec grep -il "abc" +

which is slightly faster in execution.
Sanjay Kumar Suri
Honored Contributor

Re: find a string

use grep -l "abc"

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Mark Grant
Honored Contributor

Re: find a string

How about a simple..


"cd /etc;grep abc *"

This will give you the line with the contents and the file name.

You could get cleverer though with

"grep abc * | cut -d: -f1 | sort -u"

This will give you just the filenames
Never preceed any demonstration with anything more predictive than "watch this"
Bruno Ganino
Honored Contributor

Re: find a string

This works
cd /etc
find . -type f -print -exec grep -i "" {} \;

Bye
Bruno
Torino (Turin) +2H
Mark Grant
Honored Contributor

Re: find a string

I think we've all been outshone but sanjay on this one Bruno :)
Never preceed any demonstration with anything more predictive than "watch this"
Bharat Katkar
Honored Contributor

Re: find a string

Hi,
This is how it works for me:
# ll
total 32
-rw-rw-rw- 1 root sys 2 Jul 7 16:45 one
-rw-rw-rw- 1 root sys 2 Jul 7 16:45 two
# grep 1 *
one:1
# cat one
1
# cat two
2
#

So it show me the string as well as the file name.

Regards,
You need to know a lot to actually know how little you know
florence mathon lermusi
Trusted Contributor

Re: find a string

you can also use the -print option
Shaikh Imran
Honored Contributor

Re: find a string

Hi,
Dear peterchu,
Please don't forget to assign points.
This is from your profile
"I have assigned points to 188 of 394 responses to my questions."



Regards,
I'll sleep when i am dead.
Bruno Ganino
Honored Contributor

Re: find a string

Else with this script:

cd /etc
for i in $(find . -type f)
do
tipo_fich=$(file $i|grep "abc")
if [ $? -eq 0 ];then
echo "Filename = $i">>/tmp/fich.out
grep -n "abc" $i>>/tmp/fich.out
fi
done

HTH
Bruno

Torino (Turin) +2H