Last active
December 25, 2015 06:08
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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