How do yoy recursively compare two directories in node

Viewed 2556

Id like to perform a comparison of two directories and all files within sub folders. The folder structure will be the same for both directories the files may be different. Call them directory A and directory B.

From that id like to create a directory C and directory D. All files in B that are newer than A or that are not found in A should copy over to C. Files missing from B that are found in A should be copied to directory D.

Id like to use node and either a library or run some other CLI tool like git perhaps that can do what I described without too much effort.

What would be some good approaches to accomplish this?

2 Answers

There's an npm package for this called dir-compare:

const dircompare = require('dir-compare');

const options = { compareSize: true };
const path1 = "dir1";
const path2 = "dir2";

const res = dircompare.compareSync(path1, path2, options)
console.log(res);
Related