Created
February 25, 2018 17:57
-
-
Save himanshub16/e47dd43f2b30ad77f2330d710746f5c4 to your computer and use it in GitHub Desktop.
Find N largest files in a directory (searches recursively)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
function scanDirectory(dirpath, allFiles) { | |
// nodejs runs single threaded, so is thread-safe. | |
// NO MUTEX. MISSION ACCOMPLISHED YAYY | |
if (dirpath.fullpath !== undefined) { | |
dirpath = dirpath.fullpath; | |
} | |
// had to use for...of because system calls caused exceptions as well | |
// otherwise would have used map | |
// for example, permission error for user | |
// however this should be exempted for root, which is handled well here | |
for (let eachPath of fs.readdirSync(dirpath)) { | |
try { | |
let fullpath = path.join(dirpath, eachPath); | |
let statHere = fs.statSync(fullpath); | |
let isFile = statHere.isFile() && !statHere.isSymbolicLink(); | |
let isDir = statHere.isDirectory() && !statHere.isSymbolicLink(); | |
if (isFile) { | |
allFiles.push({ | |
fullpath: fullpath, | |
filesize: statHere.size | |
}); | |
} else if (isDir) { | |
scanDirectory(fullpath, allFiles); | |
} | |
} catch (exc) { | |
console.error('error while ls for', eachPath); | |
} | |
} | |
// let everythingHere = fs.readdirSync( | |
// (dirpath.fullpath === undefined) ? dirpath : dirpath.fullpath | |
// ).map(fs.statSync); | |
// let filesHere = everythingHere.filter(each => each.isFile); | |
// let dirsHere = everythingHere.filter(each => each.isDir); | |
// console.log('files =', filesHere.length, '| dirs =', dirsHere.length); | |
// filesHere.map(allFiles.push); | |
// dirsHere.map(subdirpath => scanDirectory(subdirpath, allFiles)); | |
} | |
async function main() { | |
let rootPath = process.argv[2]; | |
let N = process.argv[3] === undefined ? 10 : process.argv[3]; | |
if (rootPath === undefined) { | |
console.error('Please provide a root directory to scan for'); | |
process.exit(1); | |
} | |
var allFiles = []; | |
await scanDirectory(rootPath, allFiles); | |
console.log('total files', allFiles.length); | |
// finding largest files | |
let allFilesSorted = allFiles.sort((file1, file2) => file1.filesize < file2.filesize); | |
console.log('top', N, 'largest files here'); | |
console.log(allFilesSorted.slice(0, N)); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment