Recursive Find from Command Line
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!
![Detect DOM Node Insertions with JavaScript and CSS Animations]()
I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel's shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API.
![Create Namespaced Classes with MooTools]()
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event...
![Create Spinning, Fading Icons with CSS3 and MooTools]()
A goal of my latest blog redesign was to practice what I preached a bit more; add a bit more subtle flair. One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.) I...
![CSS Rounded Corners]()
The ability to create rounded corners with CSS opens the possibility of subtle design improvements without the need to include images. CSS rounded corners thus save us time in creating images and requests to the server. Today, rounded corners with CSS are supported by all of...
Recently I’m using:
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.
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.https://vivekragunathan.wordpress.com/more-resources/cmd-line-sucks/
Everyday
grep
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…
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!
Check out Ack (http://beyondgrep.com/why-ack/).
Oh, you were filtering for JavaScript files only. That’s as easy as
:-)
Wasn’t able to execute this through the command line:
Err find: missing argument to `-exec'
https://github.com/ggreer/the_silver_searcher