Last active
November 30, 2016 03:25
-
-
Save NewAlexandria/5231103 to your computer and use it in GitHub Desktop.
This script takes a directory of files ./A with broken aliases in ./A/B and fixes all those aliases by finding the associated file in ./[A-B] and calling `ln -sf`. It will not handle dir aliases, and can be confused when a the source tree contain multiple files with the target filename. As a bonus, this script will reduce the size of directories…
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 'fileutils' | |
# these are the folders containing all your images | |
if ARGV.size == 2 | |
dir_base = ARGV[0] | |
alias_folder = ARGV[1] | |
else | |
puts "usage $0 dir_with_all_files sub_dir_containing_broken_aliases" | |
puts " or pass -d to use defaults: ~/img background" | |
puts " ATTN: this scrip will be confused by a source tree with A/alias_name.jpg and B/alias_name.jpg, where B/alias_name.jpg is the correct target" | |
if ARGV[0] && ARGV[0] == '-d' | |
dir_base = File.expand_path '~/img' | |
alias_folder = 'background' | |
else | |
exit | |
end | |
end | |
alias_path = File.join dir_base, alias_folder | |
# list of all alias file paths, dirs excluded | |
alist = Dir.glob( alias_path+'/**/*'). | |
select{|w| w.scan('.').any? } | |
puts "I will try to update #{alist.count} aliases" | |
# a list of all file paths, alias fodler contents excluded | |
flist = Dir.glob( dir_base+'/**/*' ). | |
reject{|w| w.scan(alias_path).any? } | |
puts " from #{flist.count} source files" | |
# forcably create new aliases by overwriting old files | |
alist.each do |f| | |
flist.each do |w| | |
if w.split('/').last == f.split('/').last | |
FileUtils.symlink(w, f, :force => true) | |
puts "linked #{w} to #{f}" | |
next | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note the nifty
/usr/bin/env ruby
gleaned from here