#!/bin/sh print_usage() { echo "$(basename $0) -- Convert multiple compressed gzip files to bzip2." echo "Usage: $(basename $0) [FILES]" } if [ $# = 0 ]; then print_usage exit 1 fi while [ $# -ne 0 ]; do ORIG_GZIPPED_FILENAME=${1} # Checking if input file exist. if [ ! -f $1 ]; then echo "$0: File ${ORIG_GZIPPED_FILENAME} not found." exit 1 fi # Checking if input file is a valid gzipped file. gzip -t ${ORIG_GZIPPED_FILENAME} if [ $? -ne 0 ] ; then echo "$0: File ${ORIG_GZIPPED_FILENAME} is not a valid gzip file." exit 1 fi # Obtaining uncompressed name of file FILENAME=$(gunzip -l ${ORIG_GZIPPED_FILENAME} | sed '1d' | sed 's/\(.*\)% \(.*\)/\2/') # Decompressing file to standard output and piping result to bzip2 gunzip ${ORIG_GZIPPED_FILENAME} --stdout | bzip2 --best > ${FILENAME}.bz2 if [ $? -ne 0 ] ; then echo "$0: Error converting file ${ORIG_GZIPPED_FILENAME} to bzip2." exit 1 fi # Keeping the original file's timestamp touch --reference=${ORIG_GZIPPED_FILENAME} ${FILENAME}.bz2 # Deleting original .gz file if [ -f ${FILENAME}.bz2 ]; then rm ${ORIG_GZIPPED_FILENAME} fi shift done