Recursive Find from Command Line

By  on  

Probably a dozen times a day I need to search any given project for specific code keywords.  Most of the time it's within a specific project but then there are times where I don't remember which directory or project the specific text is -- from my blog to my many Mozilla projects, I have code all over my local machine and it's oftentimes difficult to find something I need.

Most of the time I need to open my text editor and have it do the hard work of what I'm looking for but that's probably not efficient -- a more efficient tool would come from command line and thanks to CommandLineFu.com, I found the perfect command:

# Search all ".js" files for "debounce"
# Spits out file path, line number, and snippet where string appears
find . -name "*.js" -exec grep -in -H "debounce" {} \;

The command above searches files recursively to find the desired string, outputting the source file and the text which the string occurs in!

Recent Features

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    dat.gui:  Exceptional JavaScript Interface Controller

    We all love trusted JavaScript frameworks like MooTools, jQuery, and Dojo, but there's a big push toward using focused micro-frameworks for smaller purposes. Of course, there are positives and negatives to using them.  Positives include smaller JS footprint (especially good for mobile) and less cruft, negatives...

  • By
    Printing MooTools Accordion Items

    Sometimes we're presented with unforeseen problems when it comes to our JavaScript effects. In this case, I'm talking about printing jQuery and MooTools accordions. Each "closed" accordion content element has its height set to 0 which means it will be hidden when the...

Discussion

  1. Vladimir

    Recently I’m using:

    grep 'term' -r ./

    I think grep also has an option to filter on file extension too and I use it sometimes, but I don’t know it by heart.

  2. The output on this looks pretty must the same as with the functionality already built into grep using the -r and --include flags. I also tend to add the -n flag to output the line numbers as well. I believe this should line be equivalent.

    grep -rin --include="*.js" "debounce" .
  3. The find trick was one I learned at university in the 1990s, when most greps didn’t have the recursive flag. My vague recollection is that GNU grep introducing -r gave much of the competition a bit of a kick up the arse, and now it’s fairly common, but the find trick is still useful on older or more obscure Unix platforms…

  4. Jeremy

    Pretty sure you have a typo in there. “*.[js]” means “*.j” or “*.s”, which is likely to find nothing.

    • Good point — left some testing in there. Updated!

  5. Check out Ack (http://beyondgrep.com/why-ack/).

    ack debounce
    • Oh, you were filtering for JavaScript files only. That’s as easy as

      ack debounce --type=js

      :-)

  6. Isaiah

    Wasn’t able to execute this through the command line: Err find: missing argument to `-exec'

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!