# HG changeset patch # User Jeff Hammel # Date 1413483949 25200 # Node ID ab831c7621e9eee6354f9b500a88466196964f89 # Parent 7f910ce4da043589be936d41ed7610626a712efa hacky way to note duplicate files diff -r 7f910ce4da04 -r ab831c7621e9 python/find_duplicate_files.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/find_duplicate_files.py Thu Oct 16 11:25:49 2014 -0700 @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +find duplicate files in a directory +""" + +# imports +import argparse +import os +import subprocess +import sys + +# module globals +__all__ = ['main', 'Parser'] + +class Parser(argparse.ArgumentParser): + """CLI option parser""" + def __init__(self, **kwargs): + kwargs.setdefault('description', __doc__) + argparse.ArgumentParser.__init__(self, **kwargs) + self.add_argument('directory') + self.options = None + + def parse_args(self, *args, **kw): + options = argparse.ArgumentParser.parse_args(self, *args, **kw) + self.validate(options) + self.options = options + return options + + def validate(self, options): + """validate options""" + if not os.path.isdir(options.directory): + self.error("Not a directory: {}".format(options.directory)) + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line options + parser = Parser() + options = parser.parse_args(args) + + output = subprocess.check_output(['ls', '-l', options.directory]).strip() + rows = [row.strip().split() for row in output.splitlines()[1:]] + + sizes = {} + for row in rows: + size = int(row[4]) + filename = row[-1] + sizes.setdefault(size, []).append(filename) + + duplicates = {} + for size, filenames in sizes.items(): + if len(filenames) < 2: + continue + duplicates[size] = filenames + + for size in sorted(duplicates.keys()): + print ('{} : '.format(size)) + print ('\n'.join(duplicates[size])) + print ('\n') + +if __name__ == '__main__': + main()