-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdedupe.py
More file actions
executable file
·105 lines (90 loc) · 3.72 KB
/
Copy pathdedupe.py
File metadata and controls
executable file
·105 lines (90 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env python
"""
Simple duplicate file finder
Copyright (c) 2012 Garrick Peterson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import argparse
import hashlib
import os.path
import sys
def get_size(path):
return os.path.getsize(path)
def get_hash(path):
hash = hashlib.sha256()
with open(path, 'r') as inf:
hash.update(inf.read())
return hash.hexdigest()
def main(dir_path, delete=False, interactive=False):
print "Deduplicating based on file sizes..."
sizes = {}
for root, dirs, files in os.walk(dir_path):
for f in files:
f_path = os.path.join(root, f)
size = get_size(f_path)
try:
sizes[size].append(f_path)
except KeyError:
sizes[size] = [f_path]
print "Deduplicating based on hash of file contents..."
hashes = {}
for _, f_paths in sizes.items():
if len(f_paths) > 1:
for f_path in f_paths:
hash = get_hash(f_path)
try:
hashes[hash].append(f_path)
except KeyError:
hashes[hash] = [f_path]
duplicates = []
for _, f_paths in hashes.items():
if len(f_paths) > 1:
duplicates.append(f_paths)
if duplicates:
if not delete :
print "Duplicates files:"
print "\n".join([", ".join(x) for x in duplicates])
else:
for f_paths in duplicates:
o_file = f_paths[0]
duplicates = f_paths[1:]
for f_path in duplicates:
remove = True
if interactive:
c = raw_input("%s is a duplicate of %s, remove? (Y/N) "
% (f_path, o_file))
remove = c in ('Y', 'y')
if remove:
print "Removing %s" % (f_path,)
os.unlink(f_path)
else:
print "No duplicates found."
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('directory', nargs=1,
help="directory to run dedupe in")
parser.add_argument('--delete', action="store_true", default=False,
help="delete duplicate files")
parser.add_argument('--interactive', '-i', action="store_true",
default=False,
help="prompt before removing a file (no effect if "
"--delete not specified")
args = parser.parse_args()
try:
main(args.directory[0], args.delete, args.interactive)
except KeyboardInterrupt:
pass