Re: find a particular word in all the files (156 Views)
Reply
Advisor
satheeshnp
Posts: 42
Registered: ‎10-12-2010
Message 1 of 5 (156 Views)
Accepted Solution

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
Please use plain text.
Honored Contributor
Jose Mosquera
Posts: 1,013
Registered: ‎06-27-2002
Message 2 of 5 (156 Views)

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.
Please use plain text.
Honored Contributor
Manix
Posts: 518
Registered: ‎12-03-2010
Message 3 of 5 (156 Views)

Re: find a particular word in all the files

[ Edited ]

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 5 (156 Views)

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...

#
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 25,001
Registered: ‎03-06-2006
Message 5 of 5 (156 Views)

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 {} +
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation