Last active
January 8, 2016 14:41
-
-
Save floriandejonckheere/c4956ceb603a43ec988c to your computer and use it in GitHub Desktop.
Move all files from source directory to destination, overwriting files only if larger
This file contains hidden or 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 'fileutils' | |
| src = ARGV[-2] | |
| dest = ARGV[-1] | |
| unless src and dest and File.directory? src and File.directory? dest | |
| puts "Usage: mv-larger [-v] [-d] SRC DEST" | |
| exit false | |
| end | |
| src = File.expand_path src | |
| dest = File.expand_path dest | |
| verbose = ARGV.find_index('-v') ? true : false | |
| dryrun = ARGV.find_index('-d') ? true : false | |
| Dir.foreach src do |file| | |
| next if file == '.' or file == '..' | |
| src_file = File.expand_path File.join(src, file) | |
| dest_file = File.expand_path File.join(dest, file) | |
| if File.file? dest_file | |
| src_size = File.size? src_file | |
| dest_size = File.size? dest_file | |
| if src_size > dest_size | |
| FileUtils.mv src_file, dest_file unless dryrun | |
| puts "\'#{src_file}\' -> \'#{dest_file}\' (larger)" if verbose | |
| elsif src_size < dest_size | |
| puts "Kept #{dest_file} (larger)" if verbose | |
| else | |
| puts "Kept #{dest_file} (same size)" if verbose | |
| end | |
| else | |
| begin | |
| FileUtils.mv src_file, dest_file unless dryrun | |
| rescue StandardError | |
| puts "Error moving \'#{src_file}\' -> \'#{dest_file}\'" | |
| end | |
| puts "\'#{src_file}\' -> \'#{dest_file}\'" if verbose | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment