321

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?

meetar
  • 175
  • 1
  • 8
Nicolas Raoul
  • 10,711
  • 18
  • 64
  • 102

3 Answers3

477

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
  • 6,268
  • 2
  • 26
  • 25
  • 1
    e.g: ```unzip -o ZIP_PATH```, works as a modifier. – bonzofenix Jul 21 '14 at 22:35
  • 24
    `-o` option should be used before any other arguments. Example: `unzip -o /path/to/archive.zip -d /destination_dir` – FelikZ May 28 '15 at 08:48
  • 1
    @FelikZ Thanks for the detail, I added your (stripped down) example to the answer – foraidt May 28 '15 at 11:57
  • 2
    @loretoparisi What are you talking about? This is a Linux question. Are you looking for something like [Expand-Archive](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive)? – Franklin Yu Jan 09 '18 at 14:46
  • its showing `inflating: foldername/filename.php` @foraidt – SagarPPanchal Feb 26 '19 at 11:03
66

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

unzip -f archive.zip

But for future reference, you can just type

unzip 

and you will get a list of the arguments for this package.

The possible arguments, 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 is more suited for your needs.

Greenonline
  • 2,235
  • 11
  • 24
  • 30
Prateek Shankar
  • 761
  • 6
  • 6
  • 8
    +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 May 09 '17 at 21:23
  • 1
    Also 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 Apr 26 '18 at 07:44
  • // , The difference is in the behavior if the file doesn't exist – Nathan Basanese May 24 '18 at 18:53
  • 1
    @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 Feb 22 '19 at 21:10
2

Try using

unzip -o <filepath/zipfile.zip> -d <path where you want files>

-o is replace existing file, best is use unzip --help

Pavn
  • 21
  • 2