#!/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"