From ec5ac46e1fa894bef62bd0c65d4f5fac03e6df4d Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 9 Mar 2012 21:27:31 +0000 Subject: Always keep the copy in the last directory, if possible --- dupfiles | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/dupfiles b/dupfiles index ad189a8..61b0d4d 100755 --- a/dupfiles +++ b/dupfiles @@ -216,7 +216,7 @@ class Dupfiles(cliapp.Application): if self.settings['make-hardlinks']: self.make_hardlinks(duplicates) elif self.settings['remove']: - self.remove_all_but_one(duplicates) + self.remove_all_but_one(duplicates, args[-1]) else: self.report(duplicates) @@ -236,8 +236,17 @@ class Dupfiles(cliapp.Application): os.remove(pathname) os.link(canonical, pathname) - def remove_all_but_one(self, duplicates): - keep = duplicates.pop() + def remove_all_but_one(self, duplicates, last_dir): + if not last_dir.endswith(os.sep): + last_dir += os.sep + for pathname in duplicates: + if pathname.startswith(last_dir): + keep = pathname + duplicates.remove(keep) + break + else: + keep = duplicates.pop() + for pathname in duplicates: if self.settings['no-act']: self.output.write('rm %s\n' % pathname) -- cgit v1.2.1