Linux

tar Command Builder

Build a tar command for creating, listing or extracting, and catch an archive name that contradicts its compression.

Operation

-c, writes a new archive

The safe default. The filename implies gzip.

Members after it are read relative to this, which is how you archive a directory's contents without recording its parent path.

Members

One per line. An absolute path makes tar strip the leading slash and say so — deliberate, since it stops a later extract overwriting the real /etc.

Excludes

Emitted before the member list, because GNU tar only applies an exclude to the members named after it. Placed last, it silently excludes nothing.

Options

Without it, tar follows into any mount under the path.

Stores the file contents rather than the link, which makes the archive larger but portable.

Keeps the raw uid/gid instead of resolving them through the local user database. Wanted when restoring onto a different machine.

Command
tar -czvf backup.tar.gz --totals --exclude='*.log' --exclude=.git /var/lib/app
  • MediumArchiving an absolute path makes tar print "Removing leading `/' from member names" and store them relative. That is deliberate — it stops an extract overwriting the real /etc — so extract from the intended parent directory rather than passing -P.
  • Lowgzip is the safe default. xz compresses considerably better and costs far more time and memory; zstd is close to gzip's speed at xz-like ratios and is the better choice where it is available on both ends.
  • Info--exclude is emitted before the member list because GNU tar only applies it to members named after it. Put it last and it silently excludes nothing.
  • LowWithout --one-file-system, tar will follow into any mount under the path — /proc, /sys, a network share or another disk. For a whole-directory backup that is rarely intended.

Presets