Created
May 9, 2015 00:45
-
-
Save yadomi/a222f3de2a7966dece4b to your computer and use it in GitHub Desktop.
Rename file or files in directory with md5 hash file
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 | |
require 'digest/md5' | |
def md5_rename_file(filepath) | |
newname = Digest::MD5.file(filepath).hexdigest + File.extname(filepath) | |
puts filepath + ' -> ' + newname | |
File.rename(filepath, newname) | |
end | |
def md5_rename_directory(directory, recurive = false) | |
Dir.glob(directory + '/*').each do | file | | |
if File.file?(file) then | |
md5_rename_file(file) | |
else | |
if recurive then md5_rename_directory(file, true) end | |
end | |
end | |
end | |
unless ARGV[0] then abort "usage: #{File.basename(__FILE__)} [-R] [FILE]..." end | |
if File.file?(ARGV[0]) | |
md5_rename_file(ARGV[0]) | |
else | |
if ARGV[1] === '-R' then recursive = true else recursive = false end | |
md5_rename_directory(ARGV[0], recursive) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment