| 1 | #!/bin/bash |
| 2 | # Desc: Moves sybolic link |
| 3 | # Usage: bkmvln |
| 4 | # Input: arg1: path of existing symbolic link |
| 5 | # arg2: path where link should be moved |
| 6 | # Output: None |
| 7 | # Depends: gnucoreutils 8.30 |
| 8 | # Ref/Attrib: |
| 9 | # 1. Christopher Neylan. https://stackoverflow.com/questions/8523159/how-do-i-move-a-relative-symbolic-link#8523293 |
| 10 | # 2. Tomáš Pospíšek. https://stackoverflow.com/a/30508868 |
| 11 | |
| 12 | help() { |
| 13 | echo 'usage: mv_ln src_ln dest_dir' |
| 14 | echo ' mv_ln --help' |
| 15 | echo |
| 16 | echo ' Move the symbolic link src_ln into dest_dir while' |
| 17 | echo ' keeping it relative' |
| 18 | exit 1 |
| 19 | } |
| 20 | |
| 21 | # Show help if `--help` flag given, if arg1 isn't a symbolic link, or |
| 22 | # if arg2 isn't a directory. |
| 23 | [ "$1" == "--help" ] || [ ! -L "$1" ] || [ ! -d "$2" ] && help |
| 24 | |
| 25 | set -e # exit on error |
| 26 | |
| 27 | orig_link="$1" |
| 28 | orig_name=$( basename "$orig_link" ) |
| 29 | orig_dest=$( readlink -f "$orig_link" ) |
| 30 | dest_dir="$2" |
| 31 | |
| 32 | ln -r -s "$orig_dest" "$dest_dir/$orig_name" |
| 33 | rm "$orig_link" |