Skip to content

Instantly share code, notes, and snippets.

@hh10k
Last active December 25, 2015 06:08
Show Gist options
  • Save hh10k/6929255 to your computer and use it in GitHub Desktop.
Save hh10k/6929255 to your computer and use it in GitHub Desktop.
How long will it take for a file to reach a target size?
#!/bin/bash
FILE=$1
TARGET_SIZE=$2
SIZES=()
INTERVAL=5
CURRENT_SIZE=($(wc -c "$FILE"))
while [[ $CURRENT_SIZE -lt $TARGET_SIZE ]]; do
SIZES=("${SIZES[@]}" $CURRENT_SIZE)
OLDEST_SIZE=${SIZES[0]}
COMPLETE_PERCENT=$((100 * $CURRENT_SIZE / $TARGET_SIZE))
if [[ $CURRENT_SIZE -le $OLDEST_SIZE ]]; then
BYTES_SEC=0
ETA="Unknown"
else
BYTES_SEC=$((($CURRENT_SIZE - $OLDEST_SIZE) / ((${#SIZES[@]} - 1) * $INTERVAL)))
REMAINING_TIME=$((($TARGET_SIZE - $CURRENT_SIZE) / $BYTES_SEC))
ETA_SECONDS=$(($(date +%s) + $REMAINING_TIME))
ETA=$(date -d @$ETA_SECONDS 2>/dev/null || date -r $ETA_SECONDS)
fi
echo -ne "\r\e[K$COMPLETE_PERCENT% : $BYTES_SEC bytes/sec : ETA $ETA"
if [[ ${#SIZES[@]} -ge 6 ]]; then
SIZES=("${SIZES[@]:1}")
fi
sleep $INTERVAL
CURRENT_SIZE=($(wc -c "$FILE"))
done
echo -e "\r\e[KFinished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment