Recursively compress files in a directory and subdirectories using command line "zip" tool in Mac OS X and exclude .DS_Store files from ALL subfolders

Add a wildcard to the exclude option

zip -r myarchive.zip . -x "*.DS_Store"

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