diff --git a/bin/cp-progress b/bin/cp-progress new file mode 120000 index 0000000..5d8f7fd --- /dev/null +++ b/bin/cp-progress @@ -0,0 +1 @@ +../files/cp-progress \ No newline at end of file diff --git a/files/cp-progress b/files/cp-progress new file mode 100755 index 0000000..0615d5b --- /dev/null +++ b/files/cp-progress @@ -0,0 +1,54 @@ +#!/bin/bash + +set -e +function helpexit() { + echo Usage: $( basename "$0" ) [--nc] source [source] target + echo Copy files with a progress bar to a folder. + echo This command always + echo '* recurses to folders!' + echo '* overwrites existing files' + echo '--nc Dont count bytes first' + exit +} + +SRC=( ) +for ((i=1; i<=${#@}; i++)) { + [[ "${!i}" = "-h" ]] && helpexit + [[ "${!i}" = "--help" ]] && helpexit + [[ "${!i}" = "--nc" ]] && { NOCOUNT=1; continue; } +# [[ "${!i}" = "-"* ]] && helpexit + SRC+=( "${!i%/}" ) +} +[[ "${#SRC[@]}" -lt 2 ]] && helpexit +which pv &> /dev/null || { echo No \'pv\' installed; exit 1; } +TGT=${SRC[${#SRC[@]}-1]} +unset 'SRC[${#SRC[@]}-1]' +for path in ${SRC[@]}; do + [[ -e "$path" ]] || { echo $path missing; exit 1; } +done + +getsize() { + if [[ "$NOCOUNT" -eq 1 ]]; then + echo 0 + else + fastdu -s "${SRC[@]}" + fi +} + +# if 1 input, and is file, copy as file + +if [[ ${#SRC[@]} -eq 1 ]]; then # only one input + if [[ -f ${SRC[0]} ]]; then # its a file + if [[ ! -d "$TGT" ]]; then # target is not a dir + pv "${SRC[0]}" > "$TGT" + chmod --reference="${SRC[0]}" "$TGT" + chown --reference="${SRC[0]}" "$TGT" + exit $? + fi + fi +fi + +# Otherwise, create folder, and copy in it +[[ -f "$TGT" ]] && { echo Target can only be a folder; exit 1; } +mkdir -p "$TGT" +tar -c "${SRC[@]}" | pv -s $( getsize ) | tar -x -C "$TGT"