Dumb Stuff to Do With Grep

Get less dumberSometimes, all it takes is a little command-line snafu to bring a programmer's faith in himself crashing to the ground like an airliner suffering metal fatigue.

Today at work, I've been engaging in the wonderfully mechanical task of updating some open source libraries we use in the product. All I had to do was find which build files referenced the jars, and bump the numbers to whatever maven had in the online repos.

Easy, I thought, just a quick grep for the package name. I'll be done before lunch. Thinking I was being dead clever, I redirected the output to a textfile I could tail and further search at my leisure. So I tapped out the following:

grep -r "com.somedude.a.library" * > refs.txt

And waited for it to return, while I went off and did a few other bits and pieces (many of which involved Facebook). There are no points if you've already guessed what happened next.

An hour later, it was still running. I figured it was just down to the size of our source tree and continued to wait. Then everything on my computer ground to a halt and my viewing of a rather interesting TED talk was interrupted by Chrome complaining that the plugin had crashed, then going all "Aw, snap!" on me.

I went over to where my file was happily growing and saw that it had been gorging to a morbidly obese FIFTY GIG. Killing the call to grep (and indeed the whole console), I took a peek inside the file, which consisted primarily of lines and lines of:

refs.txt: com.somedude.a.library
refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library
refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library
refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library
refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library refs.txt: com.somedude.a.library

So my tip for today is:

Don't redirect grep output into the folder you're grepping!