+#!/bin/bash
+
+# Convert multiple compressed gzip files to bzip2.
+# Usage: gztobz2 [FILES]
+gztobz2()
+{
+ if [ $# = 0 ]; then
+ echo "$0: -- Convert multiple compressed gzip files to bzip2."
+ echo "Usage: $0: [FILES]"
+ return 1
+ fi
+
+ while [ $# -ne 0 ]; do
+ local ORIG_GZIPPED_FILENAME=${1}
+
+ # Checking if input file exist.
+ if [ ! -f $1 ]; then
+ echo "$0: File ${ORIG_GZIPPED_FILENAME} not found."
+ return 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."
+ return 1
+ fi
+
+ # Obtaining uncompressed name of file
+ local 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."
+ return 1
+ fi
+
+ # Keeping the original file's timestamp
+ touch --reference=${ORIG_GZIPPED_FILENAME} ${FILENAME}.bz2
+
+ # Deleting original gzipped file
+ if [ -f ${FILENAME}.bz2 ]; then
+ rm ${ORIG_GZIPPED_FILENAME}
+ fi
+
+ shift
+ done
+}