Created
March 20, 2019 09:41
-
-
Save heatherleaf/63a4c72e0a6f93c80103201176ec3547 to your computer and use it in GitHub Desktop.
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 | |
# | |
# vtar - "visual tar with a progress bar" | |
# by Peter Ljunglöf, 2019 | |
# inspired by: http://www.campisano.org/wiki/en/Script_bkp.sh | |
show_usage() { | |
echo | |
echo "Visual tar with a progress bar. Usage:" | |
echo | |
echo "Archiving:" | |
echo " "`basename $0`" -zcf <FILE.tgz> <DIRECTORY>" | |
echo " "`basename $0`" -cf <FILE.tar> <DIRECTORY>" | |
echo "Dearchiving:" | |
echo " "`basename $0`" -zxf <FILE.tgz>" | |
echo " "`basename $0`" -xf <FILE.tar>" | |
} | |
###################################################################### | |
# settings, change if you want | |
TAR=gtar # change this to "tar" or "gnutar" or whatever you like | |
WIDTH=60 # width of the progress bar (in characters) | |
###################################################################### | |
# here's the main code | |
if [[ ! "$1" =~ ^-z?[xc]f$ ]]; then | |
echo "Error: Wrong option: $1" >&2 | |
show_usage >&2 | |
exit 1 | |
fi | |
# get the last argument, which should be a file/directory | |
for file; do true; done | |
if [ ! -f $file ] && [ ! -d $file ]; then | |
echo "Error: File/directory does not exist: $file" >&2 | |
show_usage >&2 | |
exit 1 | |
fi | |
echo "Visual tar with a progress bar, calculating size... " | |
sizeKB=`du -sk ${file} | cut -f 1` | |
sizeMB=`echo ${sizeKB}/1024 | bc` | |
checkpoint=`echo ${sizeKB}/${WIDTH} | bc` | |
echo -n "Estimated: [" | |
for ((i=0; i<$WIDTH; i++)); do echo -n "="; done | |
if [ $sizeMB -ge 5 ] | |
then echo "] ${sizeMB}M" | |
else echo "] ${sizeKB}K" | |
fi | |
echo -n "Progress: [" | |
$TAR --record-size=1K --checkpoint="${checkpoint}" --checkpoint-action="ttyout=>" "$@" | |
echo "]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should work on Linux and Mac.
If you're using Mac, you must install GNU tar, e.g., via Homebrew:
brew install gnu-tar