tar create archives with --exclude patterns

Pack directories with tar -czf while skipping logs, dependencies and cache directories using --exclude.

LinuxbackuparchivetarLow risk
Run tar -czf OUT.tar.gz --exclude='PATTERN' DIR to create a gzipped archive that skips everything matching the exclude globs.

01Command

Command
$ tar -czf backup.tar.gz --exclude='*.log' --exclude='node_modules' /var/www/site

02What 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

RiskLow risk
!Warning: Exclude patterns match full member paths — use --exclude='dir/node_modules' or anchor patterns carefully or you may exclude more than intended.

04Undo

Undo
$ tar -xzf backup.tar.gz -C / (extracts back to original paths)

05Sources

  • tar(1) — Linux manual page
    man7.org · technical · 2026-09-17
    Evidence: Documents -c create, -z gzip filter, -f file, and --exclude=PATTERN with glob matching against member names
  • tar(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    Evidence: --exclude=PATTERN excludes files matching the glob pattern during archive creation

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions