ZIP archives produced on Mac OS X systems usually include a __MACOSX directory which is useless, annoying, and unwanted on pretty much every other OS.

For unpacking ZIP archives I use Info-ZIP, which I think is the default unzipper on most Linux distributions. What's the best way of having Info-ZIP's unzip always and automatically suppress the extraction of any __MACOSX directory which may be present?

According to the unzip man page, the -x option can be used to exclude directories from processing, and indeed adding -x __MACOSX/* to the end of my unzip command line does the trick. But I don't want to have to type this all the time. The man page also says that command-line options can be read from the UNZIP environment variable, but apparently this works only for those options which come before the archive name (whereas -x must come after it):

$ export UNZIP="-x __MACOSX/*"
$ unzip foo.zip
unzip:  cannot find or open __MACOSX, __MACOSX.zip or __MACOSX.ZIP.

Is there any better solution than wrapping unzip in a shell script which automatically adds -x __MACOSX/* to the end of the command line?


Use a shell function:

myunzip() {
    unzip "$@" -x '__MACOSX/*'

myunzip foo.zip
  • Excellent solution! Thank you! However, I modified mine: my function is called unzip, it calls /usr/bin/unzip and the first argument is $@. That way, my habit of calling unzip remains untouched and $@ does proper escaping of arguments. I don't know what "@$" does. Aug 6 '19 at 17:22
  • It was a typo for $@.
    – Barmar
    Aug 6 '19 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.