Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find help 2

Status
Not open for further replies.

sm42

Technical User
Dec 11, 2003
133
GB
Hello

What's the command to grep for a particular text in a directory?
I'm looking for text in a number of files and subdirectories.

 
Go to the top directory that you want to grep for particular text and try command:

find . -type f | grep <Text>

 
That will only see if the text is in the name of the file, not inside the file. This will examine the files for the text...
Code:
find /startingdir -type f -exec grep <Text> {} \; -print
The path and name of the file will printed after the lines found with the text in them.

Hope this helps.
 
Oh SamBones, thanks the last "-print". Find arguments are so convoluted, I did not know you could do that!

gene
 
The trick to [tt]find[/tt] is, each argument is executed left to right, only as far as an argument evaluates as true. As soon as one of the arguments evaluates to false, it stops and goes to the next file found. In other words, by having the "[tt]-print[/tt]" after the "[tt]-exec grep ...[/tt]", it will only print the file name if the grep found something. If the text isn't in the file, the file name isn't printed. If the "[tt]-print[/tt]" was put before the "[tt]-exec grep ...[/tt]", then it would print every single file from the starting directory down, with the text from the grep following only the filenames that had it. That can be a lot of output.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top