Skip to content

Instantly share code, notes, and snippets.

@unhammer
Last active April 13, 2023 14:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unhammer/b0ab6a6aa8e1eeaf236b to your computer and use it in GitHub Desktop.
Save unhammer/b0ab6a6aa8e1eeaf236b to your computer and use it in GitHub Desktop.
Show progress of some command you forgot to pipe through pv
#!/bin/bash
### Save as "/usr/local/bin/progress" and chmod +x, then:
#
# $ progress ~/huge-file.tar.bz2
# $ progress -p $(pidof bzcat)
# $ progress # progress of all open files, shows e.g. how far mpd is through your song :P
#
# source: http://stackoverflow.com/a/238140/69663
set -e -u -o pipefail
exec lsof -o0 -o "$@" |
awk '$4 ~ /^[0-9]+r$/ && $5 == "REG" && $7 ~ /^0t/ {
offset = substr($7, 3)
fname = $0; sub(/([^ \t]+[ \t]+){8}/,"",fname) # $9 to NF, but keeping IFS
"stat -c %s '\''" fname "'\''" | getline
len = $0
if (len) {
print fname, offset / len * 100 "%"
}
}' || echo "lsof returned $? for args '-o0 -o $@'" >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment