Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # make-xz v1.11 - simple frontend for tar & xz with a nice progress bar. Usage: make-xz.sh [path to source file or dir] [path to target file]
- # Source path must lead to a file or directory. It then decides whether to use tar in addition or not. If no target is given the source path is
- # used for the archive. The xz or tar.xz suffixes will be added automatically if not present in the target file name. Use the $COMPRESS variable
- # to set compression method and strength. You may use presets from 0 to 9 and add "e" for a more extreme compression or use custom options.
- . progress.sh
- ERROR(){
- printf "\033[0;31mError - $1\033[0;32m\n"
- exit 1
- }
- [ -z "$1" ] || [ ! -e "$1" ] && ERROR "Wrong input path or file name or none at all."
- LOG=/media/ramdisk/log_$(date +%y%m%d%H%M%S%3N)
- PROGRESS_MAX1=$(($(du -s -- "$1" | cut -f1)/102))
- PROGRESS_MAX2=1000
- PROGRESS_R1=" "
- PROGRESS_FT=T
- PROGRESS_MSG=" Compressed file size compared to original: "
- #COMPRESS=" --lzma2=dict=256MiB,lc=0,lp=0,pb=2,mode=normal,nice=273,mf=bt4,depth=512"
- COMPRESS=9e
- export XZ_DEFAULTS="-T0 -vvcz$COMPRESS"
- [ -d "$1" ] && SUFFIX=.tar.xz || SUFFIX=.xz
- if [ -z "$2" ]; then
- OUT=$(printf "$1" | sed 's:/*$::')$SUFFIX # remove all trailing slashes & add suffix
- else
- FILENAME=${2%*$SUFFIX}
- [ -z ${2#$FILENAME*} ] && OUT="$2"$SUFFIX || OUT="$2"
- fi
- if [ -e "$OUT" ]; then
- read -r -p "Output file already exists. Overwrite (y/N)? " OPT
- case $OPT in
- y|Y)
- rm -f "$OUT"
- ;;
- *)
- ERROR "Error: Unable to write output file."
- ;;
- esac
- fi
- printf "Compressing file"
- # XZ triggers a SIGALRM every second as an indicator to print its progress output to stdout.
- # However, by redirecting XZ's progress output the SIGALRM is no longer sent preventing XZ from emitting any progress info.
- # With the following code the SIGALRM is sent "manually" making XZ print its progress again and wherever we want.
- {
- if [ $SUFFIX = .xz ]; then
- echo " with XZ..."
- xz "$1" > "$OUT" & XZ_PID=$!
- else
- echo "s with TAR-XZ..."
- tar -JC "$1/.." "$(basename "$1")" -cf "$OUT" &
- while [ -z $XZ_PID ]; do # wait for XZ's PID to appear in the process list
- sleep 0.2s
- XZ_PID=$(ps -e | grep ' xz' | awk -F' ' '{$1=$1;print $1}')
- done
- fi
- while sleep 0.2s; do
- kill -ALRM "$XZ_PID" || break
- done
- wait "$XZ_PID"
- } 2>$LOG &
- while ! grep -qs "MiB = " $LOG; do sleep 0.2s; done # wait for XZ to stream it's output
- while [ -n "$(ps -e | grep ' xz')" ]; do
- # The long SED filter chain at the end strips the output down to a clean number by removing all leading zeros
- # and any other unwanted junk while preserving the value if it's just zero.
- PROG=$(tail -n1 $LOG | cut -d"/" -f2 | awk -F 'MiB = ' '{print $1}' | sed 's/[^0-9]*//g;s/^0*\([1-9]\)/\1/;s/^0*$/0/')
- RATIO=$(tail -n1 $LOG | cut -d"=" -f2 | awk -F ' ' '{print $1}' | sed 's/[^0-9]*//g;s/^0*\([1-9]\)/\1/;s/^0*$/0/')
- PROGRESS_BAR $PROG $RATIO
- sleep 0.1s
- done
- PROGRESS_BAR $PROGRESS_MAX1 $RATIO
- rm -f $LOG
- printf "\033[4B\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement