Skip to content

Instantly share code, notes, and snippets.

@hrdwdmrbl
Created April 5, 2023 00:06
Show Gist options
  • Save hrdwdmrbl/dd0d284cdf186eb47fb6675c0c248766 to your computer and use it in GitHub Desktop.
Save hrdwdmrbl/dd0d284cdf186eb47fb6675c0c248766 to your computer and use it in GitHub Desktop.
This script processes jdupes output to always deletes from witin the given directory
#!/usr/bin/env ruby
# This script processes jdupes output to always deletes from witin the given directory
# Example usage: `$ jdupes -r dir1 dir2 | ./delete-left.rb -d dir1`
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: delete-left.rb [options]"
opts.on("-d", "--dir DIR", "Directory to delete files from") do |dir|
options[:dir] = dir
end
end.parse!
# Iterate through each match set
while match_set = ARGF.gets("\n\n")
# Split match set into an array of files
files = match_set.split("\n")
# Determine which file to delete
files_in_directory = files.select { |file| File.dirname(file).start_with?(options[:dir]) }
next if files_in_directory.empty? || files_in_directory.length > 1
file_to_delete = files_in_directory.first
# Delete the file
if File.delete(file_to_delete)
puts "Deleted #{file_to_delete}"
else
puts "Failed to delete #{file_to_delete}", STDERR
exit 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment