Operating System - HP-UX
1748142 Members
3538 Online
108758 Solutions
New Discussion юеВ

Re: find a particular word in all the files

 
SOLVED
Go to solution
satheeshnp
Advisor

find a particular word in all the files

I need to find a particular word in all the files present under /.

If that particular word is present in any of the files, i should know the file path.

Can you please guide me in this
4 REPLIES 4
Jose Mosquera
Honored Contributor
Solution

Re: find a particular word in all the files

Hi,

#find / -type f -exec grep -l 'your string' {} \;

Please note that "-type f" search only in regular files.

Rgds.
Manix
Honored Contributor

Re: find a particular word in all the files

Use the find command in conjunction with grep:

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


http://h30499.www3.hp.com/t5/Languages-and-Scripting/grep-recursive/m-p/3863349#M17402

HP-UX been always lovable - Mani Kalra
James R. Ferguson
Acclaimed Contributor

Re: find a particular word in all the files

Hi:

If by "word" you mean what is generally though of as a word: a string of characters that match at the beginning or ending of a line, and consist of letters, digits and the underscore, then use the '-w option':

'grep -w myword'

Also, optimize your performance, *especially* if you want to search your entire server filesystem from '/':

# find / -type -f -exec grep -w myword {} +

Note the '+' terminator for the '-exec' arguments instead of the ';'. This causes multiple arguments to be aggregated and processed together with each instantiation of the exec'ed process rather than creating one process for every file.

If you want to see all matching lines and not just the filename (as with '-l'), do:

# find / -type f -exec grep -w myword {} /dev/null +

Lastly, if you only want to search beneath '/' but don't want to visit mountpoints; that is, you want to search directories like '/etc' and '/sbin' but not '/usr' or '/var', add the '-xdev' option:

# find / -xdev -type f -exec grep -w myword {} /dev/null +

Regards!

...JRF...

#
Dennis Handly
Acclaimed Contributor

Re: find a particular word in all the files

>JRF: If you want to see all matching lines
find / -type f -exec grep -w myword {} /dev/null +

Typically you don't need that /dev/null trick with "+" since there are probably multiple files. And in any case find(1) specifically warns you not to add anything after the "{}":
find / -type f -exec grep -w myword /dev/null {} +