Skip to content
Latchkey

tar --exclude: Skip Files (Usage & CI Errors)

tar --exclude drops files matching a glob pattern from the archive.

Excluding node_modules, .git, and caches keeps artifacts small. The pitfalls are pattern matching against the stored path and getting the flag order right.

What it does

tar --exclude=PATTERN skips any member whose path matches the shell-style glob PATTERN. The pattern is matched against the archive member name, so it should reflect the path as tar sees it. --exclude-vcs is a handy shortcut that drops .git, .svn, and similar metadata.

Common usage

Terminal
tar -czf src.tar.gz --exclude='node_modules' --exclude='*.log' .
tar -czf src.tar.gz --exclude-vcs --exclude='dist' .
tar -czf src.tar.gz --exclude-from=.tarignore .
tar -czf src.tar.gz --exclude='./cache/*' .

Options

FlagWhat it does
--exclude=PATTERNSkip members matching the glob
--exclude-from=FILERead exclude patterns from a file
--exclude-vcsSkip .git, .svn, .hg, and similar
--exclude-vcs-ignoresHonor .gitignore-style ignore files (GNU)
--anchoredMatch patterns from the start of the path

Common errors in CI

On GNU tar, --exclude must appear before the paths it filters, or it is ignored: put --exclude=node_modules ahead of the trailing . Quote patterns so the shell does not expand them first: --exclude='*.log' not --exclude=*.log. If files you expected to skip still appear, your pattern likely does not match the stored prefix (e.g. ./node_modules vs node_modules); add --anchored or adjust the glob.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →