#!/bin/sh
#
# Simple file/device copy command with a progress bar.
# This is VERY handle for drive-to-drive copies and the like.
#
src="$1"
dst="$2"
if [ "$src" = "" -o "$dst" = "" ]; then
echo "missing src/dst param(s)" >&2
exit 1
fi
if [ ! -e "$src" -o ! -r "$src" -o ! -f "$src" ]; then
if [ ! -b "$src" ]; then
echo "$src: not a regular file" >&2
exit 1
fi
fi
[ -d "$dst" ] && dst="${dst%/*}/${src##*/}"
if [ -e "$dst" -a ! -b "$dst" ]; then
echo "$dst: already exists, not overwriting it" >&2
exit 1
fi
if [ -b "$src" ]; then
total_size=$(grep "\<${src##*/}\$" /proc/partitions | awk '{print $(NF-1) * 1024}')
else
total_size=$(stat -c '%s' "$src")
fi
if [ "$total_size" = "" ]; then
echo "$src: unable to determine total size, aborting"
exit 1
fi
echo "Copying \"$src\" to \"$dst\":"
strace -q -ewrite cp -- "$src" "$dst" 2>&1 |\
awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i = 0; i <= percent; i++)
printf "="
printf ">"
for (i = percent; i < 100; i++)
printf " "
printf "]\r"
}
}
END { print "" }' total_size=$total_size count=0