Here is a simple solution using the core Nodejs fs libraries in conjunction with an asynchronous library. It is completely asynchronous and should work just like the du command.
var fs = require('fs'), path = require('path'), async = require('async'); function readSizeRecursive(item, cb) { fs.lstat(item, function(err, stats) { if (!err && stats.isDirectory()) { var total = stats.size; fs.readdir(item, function(err, list) { if (err) return cb(err); async.forEach( list, function(diritem, callback) { readSizeRecursive(path.join(item, diritem), function(err, size) { total += size; callback(err); }); }, function(err) { cb(err, total); } ); }); } else { cb(err); } }); }
loganfsmyth
source share