Skip to content

Instantly share code, notes, and snippets.

@javidcf
Last active August 29, 2015 14:01
Show Gist options
  • Save javidcf/57b19d2650a887cde3c7 to your computer and use it in GitHub Desktop.
Save javidcf/57b19d2650a887cde3c7 to your computer and use it in GitHub Desktop.
cpbar: cp with a progress bar
#!/bin/sh
# cpbar: cp with a progress bar
MAXCOLS=80
SLEEP=.5
if [ "$#" -ne 2 ]; then
echo "Usage: $(basename $0) <SOURCE> <DEST>" >&2
exit 1
fi
SRC="$1"
DST="$2"
SIZE=$(du -s "${SRC}" | cut -f1)
cp -R "${SRC}" "${DST}" &
PID_CP=$!
print_progress ()
{
COPIED=$(du -s "${DST}" | cut -f1)
PERC=$(echo "(100 * ${COPIED}) / ${SIZE}" | bc)
COLS=$(tput cols)
COLS=$((${COLS}<${MAXCOLS}?${COLS}:${MAXCOLS}))
FILL=$(echo "(${COPIED} * (${COLS} - 7)) / ${SIZE}" | bc)
EMPTY=$(echo "${COLS} - 7 - ${FILL}" | bc)
echo -ne '\r'
echo -n '['
for i in $(seq ${FILL}); do
echo -n '#'
done
for i in $(seq ${EMPTY}); do
echo -n '·'
done
echo -n ']'
printf ' %3d%%' ${PERC}
}
while kill -0 ${PID_CP} >/dev/null 2>&1; do
sleep ${SLEEP}
print_progress
done
echo ''
@javidcf
Copy link
Author

javidcf commented May 26, 2014

Just drop it wherever in your PATH and make it executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment