Perl Programming

HOME HTML Tree Enhancing vi Graphical df Super Sorts CPU Hogs


About this site...
Super Sorts
Graphical df
Enhancing vi
Finding CPU Hogs

Recursively Searching Subdirectories

In software development, especially on large projects, one has a frequent need to find out quickly where every occurrence of a variable name, function name, or other string literal exists in a body of source code. Unix, being tool rich, has a handful of ways to do this, but each one has its limitations and compromises.

The default approach is to use grep, but this has its shortcomings. Getting grep to search through a directory tree is possible, but must be combined with the find command and/or xargs to make it work.

Once you have grep searching through the subdirectories, you find that it doesn't know the difference between a source file and an executable or library. As a result, grep often lists out the contents of a binary file, sending terminal-scrambling control sequences to standard output.

Enter Perl. It's relatively simple to have Perl search subdirectories, either by coding a routine for the purpose or simply having it run find and parse its output. Perl also has some operators that make it easy to check whether a file is displayable. I use:

-f $myFileTrue if $myFile is a regular file
-T $myFileTrue if $myFile is a text file

The Perl script that resulted from this effort is called Not only does it recursively search subdirectories for the string or regular expression you provide, it lists the filename and line number for each matching occurrence. It's also very fast, and won't scramble your terminal window. Download by clicking on this link.

There's also another form of the script. This one has its own algorithm for recursively searching subdirectories. The previous version uses find. If you'd rather not have it call find, or want to see how to search through subdirectories, try the second version.

Syntax <path> <pattern>

  • path: the uppermost directory from which to begin searching subdirectories.
  • pattern: the search string or regular expression for the search. If it includes blank spaces or shell substitution characters, enclose it in single quotes.

Example Output

$ ./ . Math
./tree-diagram.html:2096:`|         |    |----Mathias Ettrich'
./tree-diagram.html:2993:`|         |    |----Math Commands'
./images/index.html:26:`        return "" + hexa[Math.floor(i/16)] + hexa[i%16];'
./images/index.html:35:`        Math.floor(sr * ((step-i)/step) + er * (i/step)),'
./images/index.html:36:`        Math.floor(sg * ((step-i)/step) + eg * (i/step)),'
./images/index.html:37:`        Math.floor(sb * ((step-i)/step) + eb * (i/step)));'

The equals sign separators are there to make it easier to distinguish long lines that wrap, as they have in this example. If you prefer a more compact listing, just change the value of $show_separators from 1 to 0 near the top of the script.

That's it for this installment. Until next time...

The above text and scripts are Copyright 2003 © by Scott Chilcote.

HOME HTML Tree Enhancing vi Graphical df Super Sorts CPU Hogs