Archive for July, 2012

Ever wonder why grep is taking so long?

July 10th, 2012

This is why I love programming with Unix tools available.  Let’s say you need to grep your code (text files, remember), and it’s taking longer than, say, 1 second.  Perhaps you have a bunch of generated files or something that are hogging all of your disk space.  How do you find out?  A simple one-liner:

du --exclude=.git | sort -n

I love *nix.

du is the tool that reports on file size, and since I use git, I pass it –exclude git so that it doesn’t consider that directory (I already have grep aliased to ignore .git).  Then I pipe that into sort, using -n so that it sorts the sizes numerically, instead of alphanumerically.  The result is that it shows me the big, pointless files it’s grepping through.

And now my search is back to a happy 1 second.