User Tools

Site Tools


recursive_file_search

Table of Contents

Recursive File Search

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 =
Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xml", SearchOption.AllDirectories);

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
 myFile = ARGV[0]
 myText = ARGV[1]
 Dir['**/*' + myFile + '*'].each do |path|
  File.open( path ) do |f|
   f.grep( /#{myText}/ ) do |line|
    puts(path + " : " + line)
   end
  end
 end
end
recursive_file_search.txt · Last modified: 2019/04/01 12:07 by jimc