Jeff Sheffield Jeff Sheffield - 4 months ago 6x
Linux Question

How do I 'skip' minified JavaScript files while searching through a tree of js files?

I search a large js tree frequently for certain Tokens. Consider you were looking for all of the '.prototype' tokens in your js files. I would do something like this:

find . -name "*.js" |xargs grep "\.prototype"

Of coarse this produces a boat-load of hits on minified vendor libs. Obscuring the real results I am after.

{var d=a&&typeof a=="object"?p.extend({},a{com
f?0:typeof d.duration=="number"?d.duration:d.durat
ion in p.fx.speedsp.fx.speeds[d.duration]:p.fx.speeds._defa

results like that Ad nauseam.

So how can I detect and 'skip' the minified libs?

Note: I usually store both the minified and non-minified versions of the vendor libs so I will get the hits in the vendor libs that I am looking for.

Is there some utility out there that does it?


The not so perfect Solution: I just wrote a little bash script to do it. This script leaves off some corner cases for sure. However it works in my environment, and I have quite a few minified vendor libs that I don't want to trip over.



for fname in `find . -name "*.js"`; do
   # Test the file to see if the last line is longer than... 300
   character_count=$(tail -1 $fname |wc -m)
   if [ $character_count -gt $my_magic_number ]; then
      #echo " --> compressed: $fname"       
      grep --with-filename $1 $fname
  • Put that in a text file jsgrep
  • chmod 755 the file
  • Drop it in your path.


$ cd /home/user/workspace/path/with/js/
$ jsgrep "\.prototype"

Now gets you some sensible output:

./vendor/json2.js:        String.prototype.toJSON      =
./vendor/json2.js:            Number.prototype.toJSON  =
./vendor/bootstrap.js:  Modal.prototype.escape = function () {
./vendor/bootstrap.js:  Modal.prototype.removeBackdrop = function () {
./vendor/bootstrap.js:  Modal.prototype.backdrop = function (callback) {
./vendor/backbone-1.0.0.js:    if (protoProps) _.extend(child.prototype, protoProps);
./vendor/backbone-1.0.0.js:    child.__super__ = parent.prototype;
./vendor/backbone.localStorage.js:_.extend(Backbone.LocalStorage.prototype, {
./vendor/underscore.js:  var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;