Last active
March 30, 2018 08:41
-
-
Save cyfdecyf/796d36c548125f26de03 to your computer and use it in GitHub Desktop.
Find duplicate files between two directories and use hardlink to keep only one copy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Find duplicate files in specified directories and files and use | |
# hardlink to keep only one copy. | |
require 'digest' | |
if ARGV.length < 1 | |
name = `basename #{__FILE__}` | |
puts "Usage: #{name.chomp} <path1> [path2 ...]" | |
puts " path can be either file or directory" | |
exit 1 | |
end | |
md5path = {} | |
IO.popen("find #{ARGV.join(' ')} -type f").each_line do |path| | |
path = path.chomp | |
md5sum = Digest::MD5.file(path).hexdigest | |
duplicate_path = md5path[md5sum] | |
if duplicate_path.nil? | |
md5path[md5sum] = path | |
else | |
cmd = "ln -f #{duplicate_path} #{path}" | |
puts cmd | |
system cmd | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment