dot

packages and services management
Log | Files | Refs | README

commit 0e1433a199c7448b9a4566b2cd41ef6f6143cd50
parent db9e693b329701df9819ef132ed8840621aa5881
Author: sshbio <jd@ssh.bio>
Date:   Mon, 22 Aug 2016 11:39:29 +0200

Improved lsync

Diffstat:
Mbin/lsync | 119+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mshell/.profile | 30++++++++++++++++++++++++++----
2 files changed, 97 insertions(+), 52 deletions(-)

diff --git a/bin/lsync b/bin/lsync @@ -6,17 +6,22 @@ # \/ \___/ __\ / \/ \/ \____\ - Sync from two local dirs #==========\__,'================================================================ -[ $# -lt 2 ] || [ $# -gt 5 ] && printf '%s\n' " +help=' NAME LSync - Mirror two local dirs keeping the latest version SYNTAX - lsync [-v] [-e PATTERN] DIR1 DIR2 + lsync [-v] [-e PATTERN] DIR1 ['RULE'] DIR2 OPTIONS -e Exclude PATTERN from search, matching the whole path. -v View the output without copying anything. DIR The two directories to mirror. + RULE Rule to determine in which operation to perform. + > and < only copy, + >> and << copy and overwrite, + >>> and <<< only overwrite, + none means everything. USAGE LSync mirrors data across two directories so that both contains all the @@ -25,70 +30,88 @@ USAGE When a file exists in both directories, if they do not have the same timestamps, the most recent file overwrite the other. - There is no deletion performed. -" && exit 0 + There is no deletion performed.' + +[ $# -lt 2 ] && printf '%s\n\n' "$help" && exit 0 while [ $# -gt 0 ] do case "$1" in - '-e' ) p="$2"; shift ;; - '-v' ) v=1 ;; - * ) [ -z "$dir1" ] && dir1="$1" || dir2="$1" ;; + '-e' ) e="$2"; shift ;; + '-v' ) v=1 ;; + '>' | '>>' | '>>>' | '<<<' | '<<' | '<' ) r="$1" ;; + '-'* ) printf '%s\n\n' "$help"; exit 0 ;; + * ) [ -z "$d1" ] && d1="${1%/}" || d2="${1%/}" ;; esac shift done -[ ! -d "$dir1" ] && mkdir -p "$dir1" -[ ! -d "$dir2" ] && mkdir -p "$dir2" +printf '%s\n' "$d1" +printf '%s\n' "$d2" + +[ ! -d "$d1" ] && mkdir -p "$d1" +[ ! -d "$d2" ] && mkdir -p "$d2" copied=0; overwritten=0; directories=0; identical=0 + # Generate a path list for all the files from both directories { - find "$dir1" | cut -c $((${#dir1} + 1))- - find "$dir2" | cut -c $((${#dir2} + 1))- -} | sed 's/^\///' | sort | uniq | { - [ -z "$p" ] && tee || grep -v "$p" + find "$d1" | cut -c $((${#d1} + 1))- + find "$d2" | cut -c $((${#d2} + 1))- +} | sort -u | { + [ -z "$e" ] && tee || grep -v "$e" } | { - while read path + while read -r path do # Create directories - if [ -d "$dir1/$path" ] || [ -d "$dir2/$path" ] - then [ -z "$v" ] && mkdir -p "$dir1/$path" "$dir2/$path" - printf '\033[1;30m %s\033[0m\n' "$path" + if [ -d "$d1/$path" ] || [ -d "$d2/$path" ] + then [ -z "$v" ] && mkdir -p "$d1/$path" "$d2/$path" + printf '\033[1;30m ' directories=$(($directories + 1)) - + # Copy files that does not exist on one side - elif [ ! -e "$dir2/$path" ] - then [ -z "$v" ] && cp -p "$dir1/$path" "$dir2/$path" - printf '\033[1;32m1 > 2\033[0m %s\n' "$path" - copied=$(($copied + 1)) - - elif [ ! -e "$dir1/$path" ] - then [ -z "$v" ] && cp -p "$dir2/$path" "$dir1/$path" - printf '\033[1;32m1 < 2\033[0m %s\n' "$path" - copied=$(($copied + 1)) - + elif [ ! -e "$d2/$path" ] + then if [ "$r" = '>' -o "$r" = '>>' -o -z "$r" ] + then b=';1' copied=$(($copied + 1)) + [ -z "$v" ] && cp -p "$d1/$path" "$d2/$path" + fi + printf '\033[32%sm1 > 2\033[0%sm' "$b" "$b" + + elif [ ! -e "$d1/$path" ] + then if [ "$r" = '<' -o "$r" = '<<' -o -z "$r" ] + then b=';1' copied=$(($copied + 1)) + [ -z "$v" ] && cp -p "$d2/$path" "$d1/$path" + fi + printf '\033[32%sm1 < 2\033[0%sm' "$b" "$b" + # Overwrite files keeping the latest version - elif [ "$dir1/$path" -nt "$dir2/$path" ] - then [ -z "$v" ] && cp -pf "$dir1/$path" "$dir2/$path" - printf '\033[1;31m1>>>2\033[0m %s\n' "$path" - overwritten=$(($overwritten + 1)) - - elif [ "$dir2/$path" -nt "$dir1/$path" ] - then [ -z "$v" ] && cp -pf "$dir2/$path" "$dir1/$path" - printf '\033[1;31m1<<<2\033[0m %s\n' "$path" - overwritten=$(($overwritten + 1)) - - else printf '\033[1m1 = 2\033[0m %s\n' "$path" - identical=$(($identical + 1)) + elif [ "$d1/$path" -nt "$d2/$path" ] + then if [ "$r" = '>>>' -o "$r" = '>>' -o -z "$r" ] + then b=';1' overwritten=$(($overwritten + 1)) + [ -z "$v" ] && cp -pf "$d1/$path" "$d2/$path" + fi + printf '\033[31%sm1>>>2\033[0%sm' "$b" "$b" + + elif [ "$d2/$path" -nt "$d1/$path" ] + then if [ "$r" = '<<<' -o "$r" = '<<' -o -z "$r" ] + then b=';1' overwritten=$(($overwritten + 1)) + [ -z "$v" ] && cp -pf "$d2/$path" "$d1/$path" + fi + printf '\033[31%sm1<<<2\033[0%sm' "$b" "$b" + + else identical=$(($identical + 1)) + printf '1 = 2' fi + + unset b + printf ' %s\033[0m\n' "$path" done - printf ' \ \ %-30s \033[1;32m%5s\033[0m copied - \ \__ %-30s \033[1;31m%5s\033[0m overwritten - \_____ %-30s \033[1;39m%5s\033[0m identical - %-30s \033[1;30m%5s\033[0m directories\n' \ - '' "$copied" \ - "$dir2" "$overwritten" \ - "$dir1" "$identical" \ - '' "$directories" + printf ' \ \ %-40s \033[1;32m%5s\033[0m copied + \ \__ %-40s \033[1;31m%5s\033[0m overwritten + \_____ %-40s \033[1;39m%5s\033[0m identical + %-40s \033[1;30m%5s\033[0m directories\n' \ + '' "$copied" \ + "$d2" "$overwritten" \ + "$d1" "$identical" \ + '' "$directories" } diff --git a/shell/.profile b/shell/.profile @@ -166,13 +166,37 @@ mnt() # Mount /dev/sdb1 or $1 in /mnt esac } -sanitize() # Remove spaces and other nasty characters from $1 recursively +# +# Remove spaces and other nasty characters from $1 recursively +sanitize() { - find "$1" | while read p + find "$1" | tac | while read -r p do mv "$p" "$(printf '%s\n' "$p" | sed 's/[^[:alnum:],._/+-]/_/g')" done } +# +# Transform a separator ($1) into '/': directory and subdirectory +# +split_at() +{ + find . | while read -r f + do + mkdir -p "${f%%$1*}" + mv "$f" "${f%%$1*}/${f#*$1}" + done +} + +# +# Remove prefix from all files in current directory up to "$1" separator +# +remove_prefix() +{ + find . | while read -r f + do mv "$f" "${f#*$1}" + done +} + buildit() { [ -f './autogen.sh' ] && ./autogen.sh @@ -226,5 +250,3 @@ fi # STARTUP #------------------------------------------------------------------------------- - -printf '\n'