Skip to content

Instantly share code, notes, and snippets.

@moccos
Last active December 30, 2015 10:09
Show Gist options
  • Save moccos/52bad63081c97a183e7e to your computer and use it in GitHub Desktop.
Save moccos/52bad63081c97a183e7e to your computer and use it in GitHub Desktop.
mv -> ln
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