Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Created January 29, 2009 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmacaulay/54392 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/54392 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# mvln
#
# Usage:
#
# mvln original_location new_location [symlink_location]
#
# moves the file or directory at original_location to new_location,
# then makes a symlink to new_location at symlink_location.
#
# symlink_location defaults to original_location
#
# Example:
#
# $ mvln ~/.ssh /Volumes/knox_vault/creds/
#
# Then you'd end up with your SSH keys stored safely away,
# with a symlink to the directory's new location in its place.
#
# made by James MacAulay in 2009
# http://jmacaulay.net
require 'ftools'
def mvln(*args)
original_location, new_location, symlink_location = args[0..2]
symlink_location ||= original_location
if File.directory?(new_location)
new_full_location = File.join(new_location, File.basename(original_location))
elsif File.exist?(new_location)
puts "Are you sure you want to clobber the existing file at #{File.expand_path(new_location)} [y/N]?"
exit unless $stdin.gets =~ /^[yY]/
end
new_full_location ||= File.expand_path(new_location)
`mv #{original_location} #{new_location}`
File.symlink(new_full_location, symlink_location)
puts new_full_location
end
mvln(*ARGV) if $0 == __FILE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment