ファイルを最大限に圧縮する

#!/bin/sh
#bestcompress.sh
#各圧縮プログラムでファイルを実際に圧縮しサイズのもっとも小さな圧縮ファイルを選び出す

Z="compress"
gz="gzip"
bz="bzip2"
Zout="/tmp/bestcompress.$$.Z"
gzout="/tmp/bestcompress.$$.gz"
bzout="/tmp/bestcompress.$$.bz"
skipcompressed=1

if [ "$1" = "-a" ]; then
    skipcompressed=0
    shift
fi

if [ $# -eq 0 ]; then
    echo "Usage: $0 [-a] file or files to optimally compress" >&2
    exit 1
fi

trap "/bin/rm -f $Zout $gzout $bzout" EXIT

for name ; do
    if [ ! -f "$name" ]; then
        echo "$0: file $name not found. Skipped." >&2
        continue
    fi

    if [ "$(echo $name | egrep '(\.Z$|\.gz$|\.bz2$)')" != "" ]; then
        if [ $skipcompressed -eq 1 ]; then
            echo "Skipped file ${name}: it's already compressed."
            continue
        else
            echo "Warning: Trying to double-compress $name"
        fi
    fi

    $Z  < "$name" > $Zout  &
    $gz < "$name" > $gzout &
    $bz < "$name" > $bzout &

    wait
    
    #lsを使って圧縮前のファイルと3つの圧縮ファイルのサイズを指定した順序で出力させ
    #awkでファイルのサイズを取り出し、これを小さい順に並べ替え、最もサイズの小さな
    #ファイルの行番号を取得する
    smallest="$(ls -fl "$name" $Zout  $gzout $bzout | \
        awk '{print $5"="NR}' | sort -n | cut -d= -f2 | head -1)"

    case "$smallest" in
        1 ) echo "No space savings by compressing $name. Left as is."
            ;;
        2 ) echo Best compression is with compress. File renamed ${name}.Z
            mv $Zout "${name}.Z" 
            rm -f "$name"
            ;;
        3 ) echo Best compression is with gzip. File renamed ${name}.gz
            mv $gzout "${name}.gz"
            rm -f "$name"
            ;;
        4 ) echo Best compression is with bzip2. File renamed ${name}.bz2
            mv $bzout "${name}.bz2"
            rm -f "$name"
            ;;
    esac
done

exit 0
$ ls -l ragged.txt
-rw-r--r-- 1 anmino None 642 Dec 28  2004 ragged.txt

$ ./bestcompress.sh ragged.txt
Best compression is with compress. File renamed ragged.txt.bz2

$ ls -l ragged.txt.bz2
-rw-r--r-- 1 anmino None 378 Dec 28  2004 ragged.txt.bz2