2.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/local/bin/python3
  2. import os, sys
  3. import hashlib
  4. import argparse
  5. import humanfriendly
  6. parser = argparse.ArgumentParser(description='What the dupe!?')
  7. optional = parser._action_groups.pop()
  8. required = parser.add_argument_group('required arguments')
  9. optional.add_argument('--threshold', type=str,
  10. help='Only output files greater than \'size\', e.g. 100M')
  11. optional.add_argument('--exclude', type=str, nargs='?', action='append',
  12. help='Only output files greater than \'size\', e.g. 100M')
  13. optional.add_argument('--sizes', type=str, nargs='+',
  14. help='Only output files greater than \'size\', e.g. 100M')
  15. required.add_argument('--dir', type=str, nargs='?', required=True, action='append',
  16. help='Directory to scan. Can be issued multiple times.')
  17. parser._action_groups.append(optional)
  18. args = parser.parse_args()
  19. if args.sizes:
  20. sizes = args.sizes
  21. sizes.append('larger than '+sizes[-1])
  22. else:
  23. sizes = ['10M', '50M', '100M', '1G', '5G']
  24. sizes.append('larger than '+sizes[-1])
  25. if args.exclude:
  26. exclude=args.exclude
  27. if args.threshold:
  28. threshold = humanfriendly.parse_size(args.threshold)
  29. else:
  30. threshold = 0
  31. def findDup(parentFolder):
  32. # Dups in format {hash:[names]}
  33. dups = {}
  34. for dirName, subdirs, fileList in os.walk(parentFolder):
  35. # remove excluded dirs from list
  36. for exclude in args.exclude:
  37. subdirs[:] = [dn for dn in subdirs if dirName+'/'+dn != exclude]
  38. print(' Scanning %s...' % dirName)
  39. for filename in fileList:
  40. # Get the path to the file
  41. path = os.path.join(dirName, filename)
  42. # Calculate hash
  43. if os.path.exists(path):
  44. # Calculate hash
  45. file_hash = hashfile(path)
  46. # Add or append the file path
  47. if file_hash in dups:
  48. dups[file_hash].append(path)
  49. else:
  50. dups[file_hash] = [path]
  51. return dups
  52. # Joins two dictionaries
  53. def joinDicts(dict1, dict2):
  54. for key in dict2.keys():
  55. if key in dict1:
  56. dict1[key] = dict2[key]
  57. else:
  58. dict1[key] = dict2[key]
  59. def hashfile(path, blocksize = 65536):
  60. file_size = os.path.getsize(path)
  61. # Only hash files larger than threshold (if set)
  62. if threshold == 0 or (threshold > 0 and file_size > threshold):
  63. try:
  64. afile = open(path, 'rb')
  65. hasher = hashlib.md5()
  66. buf = afile.read(blocksize)
  67. while len(buf) > 0:
  68. hasher.update(buf)
  69. buf = afile.read(blocksize)
  70. afile.close()
  71. return hasher.hexdigest()
  72. except:
  73. pass
  74. def printResults(dict1):
  75. final = {}
  76. for size in sizes:
  77. final[size] = []
  78. del size
  79. if threshold > 0:
  80. final[threshold] = []
  81. results = list(filter(lambda x: len(x) > 1, dict1.values()))
  82. for result in results:
  83. file_size = os.path.getsize(result[0])
  84. if threshold > 0:
  85. if file_size >= threshold:
  86. final[threshold].append(result)
  87. else:
  88. count = 0
  89. while count+1 < len(sizes):
  90. try:
  91. if file_size >= humanfriendly.parse_size(sizes[count]) and file_size < humanfriendly.parse_size(sizes[count+1]):
  92. final[sizes[count+1]].append(result)
  93. except:
  94. final[sizes[-1]].append(result)
  95. count += 1
  96. if file_size < humanfriendly.parse_size(sizes[0]):
  97. final[sizes[0]].append(result)
  98. final[threshold]=[False]
  99. if len(results) > 0 and len(final[threshold]) > 0:
  100. print('___________________')
  101. print('\n\033[1;34m\033[1;34m\u25b6 Duplicates Found\033[0m\n')
  102. print(' The following files are identical. The name could differ, but the content is identical')
  103. print('___________________')
  104. new = ['0']
  105. if threshold > 0:
  106. print("\n\033[1;34m\u25b6 Files bigger than %s\033[0m" % humanfriendly.format_size(threshold, binary=True))
  107. for dupe in final[threshold]:
  108. print('___________________\n')
  109. for file in dupe:
  110. print(' %s' % str(file))
  111. print('___________________')
  112. else:
  113. for size in sizes:
  114. new.append(size)
  115. if len(final[size]) > 0:
  116. if size == 'larger than ' + sizes[-2]:
  117. print("\n\033[1;34m\u25b6 >= %s\033[0m" % (new[-2]))
  118. else:
  119. print("\n\033[1;34m\u25b6 %s to %s\033[0m" % (new[-2],size))
  120. for dupe in final[size]:
  121. print('___________________\n')
  122. for file in dupe:
  123. print(' %s' % str(file))
  124. print('___________________')
  125. else:
  126. print('\n\033[1mNo duplicate files found.\033[0m')
  127. if __name__ == '__main__':
  128. if len(sys.argv) > 1:
  129. dups = {}
  130. folders = args.dir
  131. for i in folders:
  132. # Iterate the folders given
  133. if os.path.exists(i):
  134. # Find the duplicated files and append them to the dups
  135. joinDicts(dups, findDup(i))
  136. else:
  137. print('%s is not a valid path, please verify' % i)
  138. sys.exit()
  139. printResults(dups)
  140. else:
  141. print('Usage: python dupFinder.py folder or python dupFinder.py folder1 folder2 folder3')