Operating System - HP-UX
1833954 Members
2020 Online
110063 Solutions
New Discussion

Search inside text files....

 
SOLVED
Go to solution
Christine S Feeley
Occasional Advisor

Search inside text files....

I am looking for a text file that contains a particular word in the text of the file itself, not the file name. I know the file is located in one of four rather large directory structures.

Is there a command or series of commands that will give me a list of all the files that contain this word and the files location?
5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: Search inside text files....

find /example -exec grep -l 'Where are you' {} \;

That output can be redirected to a file.

Thanks.

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
Pete Randall
Outstanding Contributor

Re: Search inside text files....

find /dir1 /dir2 /dir3 /dir4 -type f -exec grep -l "text_string" {} \;


Pete

Pete
Steve Steel
Honored Contributor
Solution

Re: Search inside text files....

Hi


find /dir -type f|xargs grep -il word


Make a subdirectory and use ln -s to make aliases for the 4 directories and you can start in one place with

find . -follow|xargs grep -il word


Either way you get the file names back


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Muthukumar_5
Honored Contributor

Re: Search inside text files....

You can do it by using grep command with find as,

grep -n '/pattern or word/' `find $PWD -name "file-pattern"`

Try to use effective file pattern else fidn will try to check binary executable files too.

or use as like,

find . -type f -print |xargs grep xyz

If you like this, find command output will be used to search patter xyz on that.

Easy to suggest when don't know about the problem!
Christine S Feeley
Occasional Advisor

Re: Search inside text files....

Thanks for all the help....the first two returned a lot of errors. The last two replies worked well, and I found the file I was looking for.