brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: zip -x keeps .DS_Store and .git out of a zip file

Shell
~/tips/tip-archive-zip-exclude-patterns

zip -r site.zip site -x '*.DS_Store' '*/.git/*'

Leaves Mac and git clutter out of a zip archive you make in the terminal.

Finder on a Mac keeps its view settings in a hidden .DS_Store file inside folders, and a project folder often has a .git directory as well. Zip that folder from the terminal and both come along for the ride, which is untidy at best and leaks your commit history at worst.

Info-ZIP’s zip, the zip command in a Linux shell and in the macOS Terminal, has -x for that:

zip -r site.zip site -x '*.DS_Store' '*/.git/*'

Everything in site goes into the zip except files ending in .DS_Store and anything inside a .git folder. In zip’s own patterns a * also matches across slashes, so *.DS_Store catches the file at every level of the tree. For a longer list, put one pattern per line in a file and use [email protected].

Quote the patterns. Without quotes your shell expands *.DS_Store itself before zip ever sees it, and you end up excluding only what happens to match in the current folder. The man page uses a backslash for the same reason, -x \*.o, which works too.

The caveat is that -x takes a list: zip reads the words after it as patterns. I would keep it at the end of the line, so a file you meant to add never ends up in the exclude list by accident. And check the result before you send it anywhere:

unzip -l site.zip | grep -i -e ds_store -e .git/

Further reading: zip(1) manual page

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link