2

How to make unzip ignore the not found files ?

When I run unzip myArchive.zip config/* and config/* does not match any file within the archive, I got a return code 11.

Is it possible to make unzip try to unzip files if they exist without failure if not? If so, how ?

I did not find any option in the unzip manual to turn it into a "shut up if you fail, bro!" mode.

Of course, I could do something like unzip myArchive.zip config/* | true but it looks really ugly to me.

Thanks for your help

EDIT : it has been identified as dupplicate. It could be. The other answer is very useful, but make the command ignore ALL errors. I expected an option/trick that simply skip unfound files quietly and let the command keep failing in other error cases (if the archive does not exist for instance).

M'sieur Toph'
  • 2,534
  • 1
  • 21
  • 34
  • 3
    Possible duplicate of [Bash ignoring error for a particular command](http://stackoverflow.com/questions/11231937/bash-ignoring-error-for-a-particular-command) – John Bollinger Aug 02 '16 at 13:48
  • 1
    The modified question (of quieting specific error messages and and exit codes) is more general and interesting than the proposed duplicate. – agc Aug 02 '16 at 15:59

2 Answers2

2

As this exit status is reserved to this single error case you could maybe:

unzip myArchive.zip config/* || \
( e=$? && if [ $e -ne 11 ]; then exit $e; fi )
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • This seems to work, but doesn't eliminate the text warnings sent to STDOUT and STDERR. (Doing that in a specific way seems troublesome, one could `grep -v` for likely error strings, but there may not be any general solution.) – agc Aug 02 '16 at 15:56
  • You're right. I could propose another solution based on temporary files to log the outputs, selectively printed and/or deleted, but it would become a bit overkill, I am afraid. Let's keep it simple. Especially for such an old dog as `zip/unzip`. – Renaud Pacalet Aug 02 '16 at 16:15
  • I do not care about output, the return code was the real problem. This is an elegant way to achieve this anyway – M'sieur Toph' Aug 02 '16 at 19:32
  • I finally 'test' the unzip of the folder with -t, get the present files list with a "grep OK" and unzip them in a second time. It works but I will probably change it for your solution... Thanks. – M'sieur Toph' Aug 02 '16 at 19:36
1

Remove the error message, the error exit code, and any messages:

unzip -qq myArchive.zip config/* 2> /dev/null || true
agc
  • 7,973
  • 2
  • 29
  • 50
  • This answer addresses the OP's original Q, but doesn't answer the OP's modified Q, which is more difficult: to eliminate the results of only a specific error, while *keeping* all other errors. – agc Aug 02 '16 at 15:50