tar create archives with --exclude patterns
Pack directories with tar -czf while skipping logs, dependencies and cache directories using --exclude.
01Command
$ tar -czf backup.tar.gz --exclude='*.log' --exclude='node_modules' /var/www/site02What it does
Most tutorials cover extraction and forget creation options: --exclude takes a shell glob matched against each member name (patterns can repeat), and its position matters — put it before the directory arguments so it applies to the create operation. Combining -c create with -z gzip and -f file is the whole story; add -C / to control what the stored paths look like, since that determines where files land on extraction.
03Risk
04Undo
$ tar -xzf backup.tar.gz -C / (extracts back to original paths)05Sources
- tar(1) — Linux manual pageEvidence: Documents -c create, -z gzip filter, -f file, and --exclude=PATTERN with glob matching against member names
- tar(1) — Arch Linux manualEvidence: --exclude=PATTERN excludes files matching the glob pattern during archive creation
Last verified: 2026-09-17 · Category: Files & Folders
06Related solutions
rsync copies over SSH, skips unchanged files, resumes interrupted transfers, and preserves permissions and timestamps.
split cuts a file into size-bounded chunks; cat concatenated in order rebuilds the original byte-for-byte.