1

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!

jrdev
  • 125
  • 2
  • 13
  • 1
    What are you expecting the variable `GZNAME` to contain? You're assigning it the output of `gzip $NAME`, but that command will (most likely) produce no output, and `GZNAME` will be empty. That is, unless you have `gzip` aliased to something like `gzip -v`, but even then you will receive the whole line of output, not just the gzipped filename. – Mike Holt Feb 13 '18 at 19:12
  • 2
    `new_name="${name}.gz"`? – Cyrus Feb 13 '18 at 19:13
  • 1
    [How to concatenate string variables in Bash?](https://stackoverflow.com/q/4181703/3776858) – Cyrus Feb 13 '18 at 19:24
  • @MikeHolt i shouldve named that better, perhaps renaming it to `GZIP` Would be more helpful. i essentially want it to just zip the original file. it shouldnt return anything, only create that .gz file – jrdev Feb 13 '18 at 19:26
  • @Cyrus when i tried that i got `file.txt.gz: not found` – jrdev Feb 13 '18 at 19:27
  • @jrdev I'm not referring to what you named it. It could be called `FOOBAR` if you want. I'm just curious why you're attempting to capture the output of the command `gzip $NAME` when it's not going to output anything. Then you try to use it later on in your `du -h $GZNAME ...` command, but since `$GZNAME` is empty, your command is really just `du -h | awk '{print $1}'` – Mike Holt Feb 14 '18 at 00:01
  • @MikeHolt now that i look at it thats really stupid.. i was assuming it would save the output ( size & filename) and i was trying to just get the size. – jrdev Feb 14 '18 at 00:33

1 Answers1

2

gzip by default preserves the original extension and by default, adds .gz as the suffix.

$ echo "what" > 1.txt
$ gzip 1.txt
$ ls 1.txt.*
1.txt.gz

If you want anything other than .gz as your extension, use -S option:

$ gzip -S .zip 1.txt
$ ls 1.txt.*
1.txt.zip

Your assignment of GZNAME=$(gzip $NAME) is wrong. $GZNAME will be null for a successful compression. If you are using the default .gz extension, just use

gzip "$NAME" && GZNAME="${NAME}.gz"
iamauser
  • 11,119
  • 5
  • 34
  • 52
  • hmm i didnt get any errors running that but when i tried to echo GZNAME i still got nothing. could it be because NAME has changed ? or is it stored as a string and not messed with when gzip runs? @iamauser – jrdev Feb 14 '18 at 00:41
  • 1
    never mind i had the variable names mixed up. thanks! it worked with the very last command! – jrdev Feb 14 '18 at 02:24