Recursive File Search
.NET
Ruby
grep
grep -r --include="*.*" searchtext .
For example, to search all C# source files for instances of the the text “TODO”:
grep -r --include="*.cs" TODO .
Here’s an example that’s a little more readable:
grep -rin --include '*.json' -e 'globalmetadata' .
You can specify multiple extensions in –include like this:
grep -rin --include=\*.{json,cs} -e 'globalmetadata' .
C
using System.IO;
string[] fileList =
.GetFiles(Directory.GetCurrentDirectory(), "*.xml", SearchOption.AllDirectories); Directory
Change the *.xml mask to whatever search pattern you want. The fileList array will contain a list of all matching files, with fully-qualified paths.
Ruby
Here’s how to implement a grep-like utility in Ruby which will recursively search subdirectories:
if (ARGV[0] == nil && ARGV[1] == nil)
puts ("Usage: rbfilesearch.rb ")
else
= ARGV[0]
myFile = ARGV[1]
myText Dir['**/*' + myFile + '*'].each do |path|
File.open( path ) do |f|
.grep( /#{myText}/ ) do |line|
fputs(path + " : " + line)
end
end
end
end