X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=functions%2Fgztobz2;fp=functions%2Fgztobz2;h=6c57f80e5642b4ef52e7c84e8994305522ba3756;hb=f3c8db3027d5dc530e1f30c88e0235975211582e;hp=0000000000000000000000000000000000000000;hpb=0fb786eca497edb316e1dfaa4a4ccec2d6b3f694;p=hvlinux.git diff --git a/functions/gztobz2 b/functions/gztobz2 new file mode 100644 index 0000000..6c57f80 --- /dev/null +++ b/functions/gztobz2 @@ -0,0 +1,49 @@ +#!/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 +}