Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gunzip without overwriting non-interactively

Tags:

bash

gunzip

I want to unzip .gz files but without overwriting. When the resulting file exists, gunzip will ask for permission to overwrite, but I want gunzip not to overwrite by default and just abort. I read in a man that -f force overwriting, but I haven't found nothing about skipping it.

gunzip ${file} 

I need something like -n in copying cp -n ${file}

like image 778
herder Avatar asked Jun 03 '14 09:06

herder


2 Answers

gunzip will prompt you before overwriting a file. You can use the yes command to automatically send an n string to the gunzip prompt, as shown below:

$ yes n | gunzip file*.gz
gunzip: file already exists;    not overwritten
gunzip: file2 already exists;    not overwritten
like image 186
dogbane Avatar answered Sep 28 '22 02:09

dogbane


Granting your files have .gz extensions, you can check if the file exists before running gunzip:

[[ -e ${file%.gz} ]] || gunzip "${file}"

[[ -e ${file%.gz} ]] removes .gz and checks if a file having its name exists. If not (false), || would run gunzip "${file}".

like image 28
konsolebox Avatar answered Sep 28 '22 04:09

konsolebox