15

I'm trying to create a ZIP file using the command line zip tool that comes with the Mac OS X Terminal. I want to recursively compress the contents of the current folder but excluding .DS_Store files. I'm trying with this:

zip -r myarchive.zip . -x .DS_Store

The -x .DS_Store excludes the .DS_Store file in the current folder, but not in the recursively added subfolders. How do I exclude all the .DS_Store files from ALL subfolders as well?

2 Answers 2

25

Add a wildcard to the exclude option

zip -r myarchive.zip . -x "*.DS_Store"
3
  • Thanks for your answer. It works, but, why? :). Could you please explain?
    – OMA
    Sep 11, 2015 at 17:43
  • The * expands to nothing or any part of a path as zip recursively compresses the files and their directories. So, ./.DS_Store then ./*/.DS_Store and so on are excluded from the archive.
    – fd0
    Sep 11, 2015 at 17:56
  • Ah, interesting. So the wildcard works differently than in the Windows world, where "*.DS_Store" would just mean "any file that ends with .DS_Store"
    – OMA
    Sep 11, 2015 at 22:03
2

Most of the times I want to exclude all hidden files, such as .DS_Store or .git or .gitignore

With one simple command you can take care of all of that and compress all files and subfolders recursively:

zip -r archive.zip folder -x "*/\.*"

In case the archive already exists, above command will overwrite existing files and add new files to the archive.

If you want to replace the archive entirely you will need the following command:

zip -FSr archive.zip folder -x "*/\.*"

Or even better, create a function in zsh to make life easier

open ~/.zshrc

Add the following code in your aliases section

function zip-visible(){
    zip -FSr $1 $2 -X -x "*/\.*"
}

Reload the zsh configuration

source ~/.zshrc

Use like this

zip-visible archive.zip folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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