0e1cec521e6770be8ce89bb119c48ed7011804e3
[BK-2020-03.git] / unitproc / bkmvln
1 #!/bin/bash
2 #
3 # inspired by https://stackoverflow.com/questions/8523159/how-do-i-move-a-relative-symbolic-link#8523293
4 # by Christopher Neylan
5
6 help() {
7 echo 'usage: mv_ln src_ln dest_dir'
8 echo ' mv_ln --help'
9 echo
10 echo ' Move the symbolic link src_ln into dest_dir while'
11 echo ' keeping it relative'
12 exit 1
13 }
14
15 [ "$1" == "--help" ] || [ ! -L "$1" ] || [ ! -d "$2" ] && help
16
17 set -e # exit on error
18
19 orig_link="$1"
20 orig_name=$( basename "$orig_link" )
21 orig_dest=$( readlink -f "$orig_link" )
22 dest_dir="$2"
23
24 ln -r -s "$orig_dest" "$dest_dir/$orig_name"
25 rm "$orig_link"