shell-base/modules/utility/dir.lib

68 lines
1.6 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
}
rsynchc() { rsync --help | grep "\-$1"; }
alias rsynch="rsynchc"
function dir_copy () {
[[ ! $(command -v 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=sudo
;;
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* --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
}
dir_size () {
if sudo ls $1 >/dev/null ; then
sudo du -h --apparent-size --max-depth=1 $1 | sort -rh
else
echo $1 not a directory
fi
}
# for dir in $(/bin/find -maxdepth 1 -name "*[cC]ache*" -type d ); do; echo $dir; done