npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

find-duplicate-files

v0.0.10

Published

Find duplicate files (recursive) in given directories

Downloads

192

Readme

find-duplicate-files

A file walk util that allows you to find duplicates in given sub directories (tree). The file walk is recursive. Duplicate means that the content of two or more files is equals (checked via md5 check sum).

The directory and all sub directories will be scanned. A given file pattern will reduce the list of scanned files.

For big (static) files it can be usefull to use a md5 mapping file. This mapping file will be created a the first run. Following runs can take the md5 from the mapping file, if an entry exists. If a new file found, the md5 will be calculated and added to the mapping file.

The md5 calculation can take a long time via network and big data sets (5 hours for 1 TB data and GBit network connection).

Usage

var fdf = require('find-duplicate-files');
fdf(path <String>, options <Object>, callback hander <function(err,groups)>);
var fdf = require('find-duplicate-files');
fdf(pathes [<String>, ...], options <Object>, callback hander <function(err,groups)>);

Options

Key | Possible values | Comment ------ | ----------------------|--------------------- silent | true / false (default) | true will skip logging (except errors) checkPattern | A regular expression for file names (/.jpg$/g). Can be null. | null means: all files md5File | Specifies a text file for md5 / file name mapping. Can be null. Default: 'md5.json'| null means: 'md5.json' in the first given directory ('path'). You can set a different name (relative or absolute path) md5SkipSaving | true / false (default) | true means: no md5 file shall be saved. md5SkipLoading | true / false (default) | true means: no md5 file shall be used (if exists).

Examples

var findDuplicateFiles = require('find-duplicate-files');
 
// Basic usage (a default callback handler will log the results):
findDuplicateFiles('/Volumes/data/ebooks', {});
 
// Sample config for ebooks (big static data set):
findDuplicateFiles(
    '/Volumes/data/ebooks',
    {
        silent: true, // No logging
        checkPattern: /\.cbz$|\.cbr$/g, // Can be null
        md5File: '_md5map.json' //will be created in '/Volumes/data/ebooks'
    },
    function(err, groups) {
    if (err) return console.error(err);
        groups.forEach(function(group) {
            // A group is a set of files with the same md5 checksum:
            group.forEach(function(item) {
                // a file item has a path and a md5 property:
                console.log(item.md5 + '\t' + item.path);
            }
        }
     });
 
// Sample config for (small) text files:
findDuplicateFiles(
     [
        '/Volumes/data/dev/abc',
        '/Volumes/movies/ger'
     ]
     {
         checkPattern: /\.html$|\.js$|\.txt$/g,
         md5SkipSaving: true, // No md5 mapping file will be written
         md5SkipLoading: true // No loading of md5 mapping file      
     },
     function(err, groups) {
         if (err) return console.error(err);
         // TODO what you want
         console.log(JSON.stringify(groups, null, 4));
     }); 

Sample callback handler

Debug only:

function(err, groups) {
  if (err) return console.error(err);
  console.log(JSON.stringify(groups, null, 4));
}); 

Writing a cleanup shell script:

function(err, groups) {
    if (err) return console.error(err);
    var cmds = [];
    groups.forEach(function(group){
        cmds.push('');
        var c = 0;
        group.forEach(function(item){
            var fullName = path.isAbsolute(item.path) ?
            item.path : path.join(options.baseDir, '/', item.path);
            // Decide if you  want to delete or not:
            if (c < group.length - 1 && item.path.indexOf('___todo') > -1) {
                cmds.push('rm -f "' + fullName + '"');
                // Windows: cmds.push('DEL /F /S /Q /A "' + fullName + '"');
                c++;
            } else {
                cmds.push('# "' + fullName + '"');
                // Windows: cmds.push('REM "' + fullName + '"');
            }
        }); // forEach
    }); // forEach
    // Write shell script to file (but don't execute it):
    var stream = fs.createWriteStream("clean.command");
    stream.once('open', function(fd) {
        cmds.forEach(function(cmd) {
            stream.write(cmd + '\n');
        });
        stream.end();
    }); // stream   
}); 

Auto delete callback handler (madness mode):

function(err, groups) {
    if (err) return console.error(err);
    groups.forEach(function(group) {
         // loop starts at index 1
         // first item will be untouched
         for (var i = 1; i < group.length; i++) {
             fs.unlinkSync(group[i].path);
         }
    }); // forEach
});