Is there a way to force gzip not to overwrite files on conflict?

The closest thing that I could find to a single command is the following:

yes n | gzip archive/foo

The yes command prints y followed by a linefeed to stdout until it receives a signal. If it has an argument, it will print that instead of y. In this case, it prints n until gzip exits, thus closing the pipe.

This is the equivalent of entering n repeatedly on the keyboard; this will automatically answer the question gzip: archive/foo.gz already exists; do you wish to overwrite (y or n)?

I general, I think that it's better not to try to compress the files if the corresponding gzipped file exists; my solution is noisier, but it fits my particular needs for a drop-in replacement for the gzip command, sitting in a configuration file.


It has dawned on me that the best way to avoid the undesired effect is to not ask the program to perform the undesired effect. That is, simply don't tell it to compress a file if the file is already present in compressed form.

e.g:

if [ ! -f "$file.gz" ]; then 
    gzip "$file"; 
else 
    echo "skipping $file"
fi

or shorter (run true if there is a file.gz, compress file otherwise)

[ -f "$file.gz" ] && echo "skipping $file" || gzip "$file"