Operating System - HP-UX
1832251 Members
2531 Online
110041 Solutions
New Discussion

How to search srings in OS?

 
SOLVED
Go to solution
lin.chen
Frequent Advisor

How to search srings in OS?

hello all,
I want to find a file with strings "hello" in it.
how can I do?use find command?thanks
9 REPLIES 9
Paul Sperry
Honored Contributor
Solution

Re: How to search srings in OS?

cd /
find . -print | xargs grep -i hello
Paul Sperry
Honored Contributor

Re: How to search srings in OS?

or just

cd /dir
grep -i hello *
Steven E. Protter
Exalted Contributor

Re: How to search srings in OS?

Shalom lin.chen,

find / -exec grep -l "hello" {} \;

find options

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Peter Godron
Honored Contributor

Re: How to search srings in OS?

Hi,
Please read:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=629412

The -type f restrict to files only, rather than for example directories etc.

Note: This can take a while to run and return some strange results, like searching binaries etc.


James R. Ferguson
Acclaimed Contributor

Re: How to search srings in OS?

Hi:

As noted, you don't want to search directories, and you don't want to search binary files. Youc can use this approach though:

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

...run the script passing the directory ($1) that you want to search and the pattern ($2) you want to find. For example:

./google /etc/rc.config.d ROUTE

Regards!

...JRF...
Frank de Vries
Respected Contributor

Re: How to search srings in OS?

I always use this for me:

passing grep to find in back quotes ` `

grep -n "hello" `find /full/path/* -type f -print`

where n is the line number.

If argument list is too long,
then loop by directory.

Let me know if you want to know how.

Look before you leap
lin.chen
Frequent Advisor

Re: How to search srings in OS?

if I do not want to search in /tmp.
how can I do
Patrick Wallek
Honored Contributor

Re: How to search srings in OS?

If you don't want to search /tmp, don't specify it in your find command.
James R. Ferguson
Acclaimed Contributor

Re: How to search srings in OS?

Hi Lin:

You can specify multiple directories to search when you use 'find':

# find /usr /var /opt -xdev -type f -print

The use of '-xdev' prevents 'find' from traversing mountpoints. This is most useful if you want to search the root directory ('/') but *not* mountpoints like '/usr', '/var' etc.

Regards!

...JRF...