#!/bin/bash # bestcompress--Given a file, tries compressing it with all the available # compression tools and keeps the compressed file that's smallest, # reporting the result to the user. If -a isn't specified, bestcompress # skips compressed files in the input stream. setglobal Z = '"compress'," gz = '"gzip'," bz = '"bzip2'" setglobal Zout = ""/tmp/bestcompress.$Pid.Z"" setglobal gzout = ""/tmp/bestcompress.$Pid.gz"" setglobal bzout = ""/tmp/bestcompress.$Pid.bz"" setglobal skipcompressed = '1' if test $1 = "-a" { setglobal skipcompressed = '0' ; shift } if test $Argc -eq 0 { echo "Usage: $0 [-a] file or files to optimally compress" > !2; exit 1 } trap "/bin/rm -f $Zout $gzout $bzout" EXIT for name in [@Argv] { if test ! -f $name { echo "$0: file $name not found. Skipped." > !2 continue } if test $[echo $name | egrep '(\.Z$|\.gz$|\.bz2$)] != "" { if test $skipcompressed -eq 1 { echo "Skipped file $(name): It's already compressed." continue } else { echo "Warning: Trying to double-compress $name" } } # Try compressing all three files in parallel. $Z < $name > $Zout & $gz < $name > $gzout & $bz < $name > $bzout & wait # until all compressions are done # Figure out which compressed best. setglobal smallest = $[ls -l $name $Zout $gzout $bzout | \ awk '{print $5"="NR}' | sort -n | cut -d= -f2 | head -1] match $smallest { with 1 echo "No space savings by compressing $name. Left as is." with 2 echo Best compression is with compress. File renamed $(name).Z mv $Zout "$(name).Z" ; rm -f $name with 3 echo Best compression is with gzip. File renamed $(name).gz mv $gzout "$(name).gz" ; rm -f $name with 4 echo Best compression is with bzip2. File renamed $(name).bz2 mv $bzout "$(name).bz2" ; rm -f $name } } exit 0