Last active
December 30, 2015 10:09
mv -> ln
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
function mvln() { | |
function __mvln() { | |
local opts=$1 | |
local src=$2 | |
local dst=$3 | |
if [ -d $dst ]; then | |
# When dst is dir, dst_file must be dir/filename | |
local file=${src##*/} | |
mv $opts -- $src $dst && ln -s -- ${dst}/${file} $src | |
else | |
mv $opts -- $src $dst && ln -s -- $dst $src | |
fi | |
} | |
### get options for mv | |
local OPTIND o opts | |
while getopts "bfv" o; do | |
case "${o}" in | |
[bfv]) | |
opts="${opts}${o}" | |
;; | |
*) | |
return | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ "${opts}" != "" ]; then | |
opts="-${opts}" | |
fi | |
### mv + ln -s | |
if [ $# -lt 2 ]; then | |
echo "mvln: too few arguments." | |
return | |
elif [ $# -eq 2 ]; then | |
__mvln "$opts" $1 $2 | |
else | |
local dst_dir | |
for dst_dir; do true; done # get the last argument | |
if [ ! -d $dst_dir ]; then | |
mkdir -p $dst_dir || return | |
fi | |
for src_file in ${@:1:${#}-1}; do | |
local dst_file=${dst_dir}/${src_file} | |
__mvln "$opts" $src_file $dst_file | |
done | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment