I'm trying to compress a file in a shell script and output its pre and post compression size. so far I take the file name as an argument, compress the file with gzip, and then grab the compressed size using the new file name. to get that new filename i am trying to add the .gz extension to the existing filename.
#!/bin/sh
#Name of the file input
NAME=$1
#Uncompressed size of the file input
UNCOMPRESSED=$(du -h $NAME | awk '{print $1}')
echo ""
echo "$NAME will be compressed using the gzip command."
echo ""
echo "gzip:"
echo "Uncompressed: $UNCOMPRESSED"
#Compress the file
GZNAME=$(gzip $NAME)
#Compressed size of the file input
COMPRESSED=$(du -h $GZNAME | awk '{print $1}')
echo "Compressed: $COMPRESSED"
How can i add the .gz extension to file name? i know this isnt right but maybe something like NEW_NAME = $($NAME + ".gz")
Everywhere i've seen is replacing the exisiting extension, but i wanna preserve the existing extension. so $NAME --> file.txt
and $NEW_NAME ---> file.txt.gz
thanks!