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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep for every occurance 1

Status
Not open for further replies.

abovebrd

IS-IT--Management
May 9, 2000
690
US
Sorry for posting this, I am sure its been answered many times, I search this forum later on

Question:

I need to search for every occurance of a specific string within every file conatined with a directory and every subdirectory.

String=192.168.1.23

I would like to output to a file listing

Thanks in advance


-Danny






 
This is what I came up with,

find ./ -exec grep -l 192.168.1.23 {} \; > /tmp/file.tmp


-Danny






 
This command will do the work:

find dir -exec grep -l 192.168.1.23 {} \;

where dir is the top directory you want to search from.

Good luck

 
Even faster:
[tt]
find /initial/directory -type f -print0 | xargs -0 grep -l '192\.168\.1\.23'
[/tt]

[tt]find[/tt] will only generate file (not directory) names and separate each name with a null then spaces won't in filenames won't affect, [tt]xargs[/tt] will take the null separated names and will run [tt]grep[/tt] the minimum needed times to process all files, givin the maximum supported command line. [tt]grep[/tt] will only show the file names (because -l) and remember the dot is special to grep then you must to escape it with a \.
I hope it works...
Unix was made by and for smart people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top