cp-progress tool

This commit is contained in:
ville rantanen
2017-05-14 14:23:34 +03:00
parent 0d8ee0b59a
commit 09f343c52f
2 changed files with 55 additions and 0 deletions

1
bin/cp-progress Symbolic link
View File

@@ -0,0 +1 @@
../files/cp-progress

54
files/cp-progress Executable file
View File

@@ -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"