Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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…
#!/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]
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'
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}"

This comment has been minimized.

Copy link
Owner Author

NewAlexandria commented Mar 24, 2013

Note the nifty /usr/bin/env ruby gleaned from here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.