Linux unzip command: Option to force overwrite?

192

23

I am writing a shell script that unzips a ZIP file into an existing hierarchy of files, potentially overwriting some of the files.

The problem is that the unzip command asks for confirmation:
replace jsp/extension/add-aspect.jsp? [y]es, [n]o, [A]ll, [N]one, [r]ename: y

Is there an option to force unzip to overwrite the files?

Nicolas Raoul

Posted 2010-01-26T08:48:23.293

Reputation: 7 766

Answers

294

According to http://www.manpagez.com/man/1/unzip/ you can use the -o option to overwrite files:

unzip -o /path/to/archive.zip

Note that -o, like most of unzip's options, has to go before the archive name.

foraidt

Posted 2010-01-26T08:48:23.293

Reputation: 4 398

1e.g: unzip -o ZIP_PATH, works as a modifier. – bonzofenix – 2014-07-21T22:35:33.413

11-o option should be used before any other arguments. Example: unzip -o /path/to/archive.zip -d /destination_dir – FelikZ – 2015-05-28T08:48:40.730

1@FelikZ Thanks for the detail, I added your (stripped down) example to the answer – foraidt – 2015-05-28T11:57:05.747

this does not work on Azure web sites (Windows). – loretoparisi – 2016-08-25T14:28:06.063

@loretoparisi What are you talking about? This is a Linux question. Are you looking for something like Expand-Archive?

– Franklin Yu – 2018-01-09T14:46:00.890

its showing inflating: foldername/filename.php @foraidt – SagarPPanchal – 2019-02-26T11:03:21.367

35

If you need to unzip in order to replace the new files only, you can use

unzip -f archieve.zip

But for future references, you can just type

unzip 

and you will get a list of the arguments for this package. The possible arguments are for this case are:

-f  freshen existing files, create none
-n  never overwrite existing files         
-q  quiet mode (-qq => quieter)
-o  overwrite files WITHOUT prompting    

Use the one that you feel more suited for you needs.

Prateek Shankar

Posted 2010-01-26T08:48:23.293

Reputation: 451

6+1, I like the more subtle, friendlier version of "here's how to rtfm for context, but here's the relevant section of the m" approach. – Nathan Basanese – 2017-05-09T21:23:12.930

1Also looked at the help, but didn't understand the diff. between -u update files, create if necessary and -f freshen existing files, create none ‍♂️ ? – juanmirocks – 2018-04-26T07:44:24.097

// , The difference is in the behavior if the file doesn't exist – Nathan Basanese – 2018-05-24T18:53:52.207

@juanmirocks With -f, the newer files will not be extracted at all even if they are not on the destination (such as if a later version of the zip is created that has files that the previous unzip did not create). If you don't already know you want that, chances are you don't. On the contrary, -u will extract additional files not on the destination, ensuring nothing is missing from the destination which is in the zip. – poikilos – 2019-02-22T21:10:58.410