1819500 Members
3164 Online
109603 Solutions
New Discussion юеВ

grep recursive

 
SOLVED
Go to solution
Francesco_13
Regular Advisor

grep recursive

Hi,

how i can find the word "force" in all text file of my hp-ux system?
grep -i force * find in executables file too..

thanks

Best regards.
Francesco
16 REPLIES 16
IT_2007
Honored Contributor

Re: grep recursive

you won't be able to find word from executables using grep command. you have to use strings first and then use grep command to find the words.
IT_2007
Honored Contributor

Re: grep recursive

like

strings /etc/lvmtab |grep "vg"

which will show you only names with vg.
Francesco_13
Regular Advisor

Re: grep recursive

Hi,
i wolud find recursively where i have the word 'force' in all my files in the system...

with you suggested command i can do it this?

thanks and regards
Pete Randall
Outstanding Contributor

Re: grep recursive

Use the find command in conjunction with grep:

find /start_dir -type f -exec grep -l "force" {} \;

Be warned, however, that binary files will do not grep well. Ideally you would need to find some way to exclude binaries, perhaps by being more selective about which directories you "find" in.


Pete

Pete
Francesco_13
Regular Advisor

Re: grep recursive

thanks to all.
Question solved.

regards.
Francesco
James R. Ferguson
Acclaimed Contributor

Re: grep recursive

Hi:

Using '-exec' with 'find' causes a separate process to be forked for every file found. This can be very detrimental to your system's performance.

Secondly, you should add '-xdev' so that you do not cross mountpoints as 'find' descends directories. This would be *very* important if you were interested in searching the root 'root' filesystem (including '/etc' which is not a separate mountpoint but excluding trees like '/usr' and '/opt' which are there own mountpoints.

Thus, at a minimum, do:

# find /path -xdev -type f | xargs grep -i force

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: grep recursive

Francesco,
variation on a theme:
grep force `find / -exec file {} \; | grep -v directory | grep -v executable | awk -F':' '{print $1}'`

This should exclude directories and execuatble as defined by file (man file)

James R. Ferguson
Acclaimed Contributor
Solution

Re: grep recursive

Hi (again) Francesco:

[ It's taken an hour to be able to post this...the Forum has been out-to-lunch ]

If you want to be able to restrict your 'grep' to only text files ( and not binary ones ) and in addition identify each file matching the pattern you specify, use:

# cat ./google
#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -xdev -type f | while read FILE
do
[ `file ${FILE} | grep -c ascii` -eq 0 ] && continue
grep "$PAT" ${FILE} /dev/null
done
exit 0

...run as:

# ./google /path pattern

for example:

# ./google /etc hostname

Regards!

...JRF...
Francesco_13
Regular Advisor

Re: grep recursive

Hi,
thanks to reply. Due a delay of forum i cannot submit points(the therad was closed..) ..i would 10 point for you!

Best regards.
Francesco

Pete Randall
Outstanding Contributor

Re: grep recursive

Francesco,

You can still assign JRF the points he deserves. Click on the "reopen thread" button, then assign the points, then click on the "close thread" button.


Pete

Pete
IT_2007
Honored Contributor

Re: grep recursive

Francesco,

Thread is not closed. Please assign points to JRF. He deserves it.

Thanks,
IT_2007
Francesco_13
Regular Advisor

Re: grep recursive

Hi,
ok, before weekend was be problematic connect with forum. This morning it's ok! assigned points.
we can close the thread.
thanks to all
regards.
Francesco
Francesco_13
Regular Advisor

Re: grep recursive

Hi,
when i run the script from / of my hp-ux, after several minutes i receive this error:
/usr/bin/grep: The parameter list is too long.
I don├В┬┤t know where have stopped.

i use:
grep -i force `find / -exec file {} \; | grep -v directory | grep -v executable | awk -F':' '
{print $1}'`

How i can remove this problem?

Thanks.

Best regards.

Francesco

Francesco_13
Regular Advisor

Re: grep recursive

Hi,

for the other option with:
#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -xdev -type f | while read FILE
do
[ `file ${FILE} | grep -ic ascii` -eq 0 ] && continue
grep "$PAT" ${FILE} /dev/null
done
exit

I have introduced -i option for uppercase. But one question: how i can do it for find only word "force" and no returning me other words how for example "forced" where force it's present?

Thanks.

Best regards.
Francesco
James R. Ferguson
Acclaimed Contributor

Re: grep recursive

Hi (again) Francesco:

A simple 'grep' will match the string of characters without regard to where they occur. A simple solution for finding only the word "force" would be:

# grep -i -e "^force " -e " force$" -e " force "

The caret (^) anchors the string to the beginning of a line. The dollar sign ($) anchors the string at the line's end. Surrounding the string with spaaces " " creates the sense of an English word.

In answer to your question regarding Peter's script, the "parameter list is too long" error arises because the 'find' is creating a list of files for input the 'grep'. The list generated is too long to be passed to 'grep' in one unit.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: grep recursive

>grep -i force `find / -exec file {} \; | grep -v directory | grep -v executable | awk -F':' '
{print $1}'`

Peter's script is not the proper way to use find, if it will return an infinite number of files. You must use xargs or better yet use -exec grep +.

It seems you can only use -exec file +. But you shouldn't use `find ...`.

You need to replace by:

find / -exec file + | \
grep -v -e directory -e executable | \
awk -F":" '{print $1}' | xargs -n40 grep -i force

You may want to add 2> /dev/null to the find and the last grep.

>how i can do it for find only word "force" and no returning me other words how for example "forced" where force it's present?

You need to use grep -w. (If you are on 11.11) This is easier than James' solution. Otherwise a more rigorous solution for 11.00 would use

[[:space:][:punct:]] where he has a blank, before and after your word "force".