Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node binary to delete duplicate files in a directory
#!/usr/local/bin/node
const crypto = require("crypto");
const fs = require("fs");
const folder = process.argv[2];
const absoluteFolder = `${__dirname}/${folder}`;
fs.readdir(absoluteFolder, (err, files) => {
const map = {};
files.forEach(filename => {
const absoluteFilePath = `${absoluteFolder}/${filename}`;
if (fs.existsSync(absoluteFilePath)) {
const input = fs.createReadStream(absoluteFilePath);
const hash = crypto.createHash("sha256");
hash.setEncoding("hex");
input.on("end", () => {
hash.end();
const hashValue = hash.read();
if (map[hashValue]) {
fs.unlink(absoluteFilePath);
} else {
map[hashValue] = true;
}
});
input.pipe(hash);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.