shell-base/modules/utility/dir.lib

57 lines
1.3 KiB
Bash

#!/bin/bash
# simple local copy wintin or merge one directory to another using rsync. Preserves everything
# present working directory to supplied destination
isDir() {
if [[ -d $1 ]]
then
echo "true"
return 0
else
return 1
fi
}
function dir_copy () {
[[ ! $(which rsync) ]] && { echo rsync must be installed to use this function; return 5; }
module_load confirm
local usesudo;local cmd;local src;local noconfirm;local merge
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'sem' OPTION; do
case "$OPTION" in
s)
usesudo=true
;;
e)
noconfirm=true
;;
m)
merge=true
;;
*)
echo unknown option $OPTION
return 1
;;
esac
done
shift $(( OPTIND - 1 ))
[ $# -ne 2 ] && echo two directories source and destination need to be passed && return 2
src=$(realpath -s $1)
dest=$(realpath -s $2)
[ ! -d "$src" ] && echo source: $src is not a directory && return 3
[[ $merge ]] && src=${src}/
cmd="$usesudo rsync --exclude *[C]ache* --exclude node_modules --progress -aAru $src $dest"
echo $cmd
[[ ! -v PS1 ]] && noconfirm=true
[[ ! $noconfirm ]] && { confirm Do you want to $([[ $merge ]] && echo merge $src into || echo copy $src within) $dest? || return 0; }
echo copying.....
eval $cmd
}