0

I want to gunzip a filename, stored in $2, to a specified directory, stored in $3. For example,

if [ -f $2 ]; then

gunzip $2

here I want to remove .gz from $2 

mv $2 $3

fi

so from the terminal command line when running a script I type:

-z anotherdirectory/test.txt user/harry

because the error says mv: cannot stat ‘backup/test.txt.gz’: No such file or directory.

Thank you everyone!!

N. Joy
  • 1
  • 1

2 Answers2

1

To remove a single extension, use the % suffix deletion operator:

mv "${2%.*}" "$3"

or if you wanted to only remove .gz, you could use:

mv "${2%.gz}" "$3"

But you could avoid the copy altogether by uncompressing the file directing where you want it to go:

gunzip -c "$2" > "$3"

The -c option tells gunzip to write to stdout. Unlike a normal gunzip, that does not remove the gzip file, so you would have to rm it afterwards.

It's worth noting that gunzip understands other extensions such as tgz; it will decompress foo.tgz to foo.tar, not foo. So the command which just deletes the extension will not work with all extensions, whereas gunzip -c will.

rici
  • 234,347
  • 28
  • 237
  • 341
0
 mv $(echo $2 |sed 's/\.gz$//') $3 

Also you should check this tutorial about sed

Gongora
  • 595
  • 6
  • 10