Skip to content

Instantly share code, notes, and snippets.

@merura
Created August 25, 2025 06:45
Show Gist options
  • Select an option

  • Save merura/387c5fa55ecf5ede58412f7e0d114c04 to your computer and use it in GitHub Desktop.

Select an option

Save merura/387c5fa55ecf5ede58412f7e0d114c04 to your computer and use it in GitHub Desktop.
sync.sh
#!/usr/bin/env bash
# Based on https://unix.stackexchange.com/a/693178
repr () {
if [[ $# -gt 0 ]]; then
local result="$(printf -- '%q ' "$@")"
printf -- '%s' "${result::-1}"
fi
return $?
}
sync () {
local args
if [[ $- = *i* ]]; then
args="$(repr "$@")"
bash -sc "$(shopt -p); $(declare -f); ${FUNCNAME[0]} $args"
return $?
fi
local initial_value current_value cleared backspace i percentage speed elapsed start_time eta
start_time=$(date +%s)
command sync "$@" &
initial_value="$(grep -e 'Dirty:' /proc/meminfo | awk '{print $2}')"
(( initial_value == 0 )) && initial_value=1
sleep 0.1
while ps -p $! > /dev/null; do
current_value="$(grep -e 'Dirty:' /proc/meminfo | awk '{print $2}')"
cleared=$(( initial_value - current_value ))
percentage=$(( cleared * 100 / initial_value ))
(( percentage < 0 )) && percentage=0
elapsed=$(( $(date +%s) - start_time ))
speed=$(( cleared / (elapsed + 1) )) # kB/s
eta=$(( (current_value) / (speed + 1) )) # seconds
printf -- '\r%3d%% | %d kB/s | ETA: %ds' "$percentage" "$speed" "$eta"
sleep 1
done
printf -- '\r%3d%% | %d kB/s | ETA: 0s\n' 100 "$speed"
wait $!
}
sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment