How-to

The tar Command, From Basics to Incrementals

Tar has been bundling files since 1979 and still does the job on every server. This reference covers its modes, flags and the lesser-known features that save time.

What does tar do?

It packs files and folders, with their names, permissions, owners, timestamps and links, into a single stream called an archive, and unpacks them again. The name comes from "tape archive", its original purpose. Tar itself does not compress; it hands the stream to gzip, zstd, xz or bzip2 when asked. GNU tar on Ubuntu, Debian and the Enterprise Linux images, and bsdtar on FreeBSD 14, share the common flags shown here.

What are the main modes?

Every tar command picks exactly one mode:

ModeShortDoes
--create-cMake a new archive
--extract-xUnpack files
--list-tShow contents
--append-rAdd files to an uncompressed archive
--update-uAppend only files newer than their archived copy
--diff-dCompare archive with the file system
--deleteRemove members from an uncompressed archive (GNU)

Add -f name to point at the archive file, -v for verbose output and -C dir to change folder first. Old-style bundled letters like tar czvf without a dash still work, but the dash form is clearer.

Which compression flags are there?

-z for gzip, -j for bzip2, -J for xz and --zstd for zstd. -a picks the compressor from the output file's extension, so tar -caf logs.tar.zst /var/log/nginx produces a zstd archive without remembering the flag. For any other program, or extra options, use -I: tar -I 'zstd -T0 -12' -cf .... The trade-offs between formats are compared in Create tar.gz archives.

How do I add or compare files?

Appending works only on uncompressed archives, since compressed streams cannot be extended in place:

tar -rf handover.tar notes/runbook.md
tar -uf handover.tar -C /etc nginx
tar -df handover.tar -C / 2>&1 | head

The last line compares the archive with the live files and prints differences such as "Mod time differs" or "Size differs", a quick way to see what changed since a configuration snapshot.

How do incremental backups work with tar?

GNU tar keeps a snapshot file that records what it saw last time. The first run with a new snapshot file makes a full backup; later runs with the same file store only changes, including deletions:

tar --listed-incremental=/var/backups/etc.snar -czf /var/backups/etc-full.tar.gz -C / etc
tar --listed-incremental=/var/backups/etc.snar -czf /var/backups/etc-inc-$(date +%F).tar.gz -C / etc

To restore, extract the full archive, then each incremental in order with --listed-incremental=/dev/null. Copy the snapshot file before each run if you want several levels. For most servers, rsync snapshots are friendlier, as shown in Back up data with rsync, but tar incrementals produce single files that are easy to ship anywhere.

Which lesser-known options are worth knowing?

  • --sparse (-S): stores sparse files such as VM disk images efficiently instead of writing out the empty space.
  • --checkpoint=10000 --checkpoint-action=dot: prints a dot every 10,000 records, a cheap progress indicator on huge archives.
  • --one-file-system: stays on one mount, so archiving / skips /proc, /sys and mounted data drives.
  • --acls --xattrs --selinux: keeps access lists, extended attributes and SELinux labels on Enterprise Linux systems.
  • --remove-files: deletes originals after they are archived, useful for rolling up old logs.
  • --transform='s,^old,new,': renames paths on the way in or out with a sed expression.
  • --files-from=list.txt: archives exactly the paths listed in a file, one per line.

How do I archive logs older than a week?

Combine find with --files-from and --remove-files:

cd /var/log/app && find . -name '*.log' -mtime +7 -print0 \
  | tar --null -czf /var/archive/app-logs-$(date +%Y%m%d).tar.gz --files-from=- --remove-files

-print0 and --null make the pipeline safe for file names that contain spaces.

How fast is tar on a dedicated machine?

Tar itself is rarely the bottleneck; the compressor and the disks are. On NVMe drives, as on every model from S1 upward, an uncompressed tar of a large folder runs at well over a gigabyte per second. Compression then uses one core with gzip or many with pigz and multi-threaded zstd. On a box like X2 with 96 cores, zstd -T0 keeps pace with the drives.

For extraction specifics, see Extract .tar.gz files and Extract .tar.bz2 files.

Keep Reading

Create tar.gz archives

Pack folders into tar.gz archives, choose compression levels, use pigz on many cores and compare gzip with zstd and xz.

Open

Extract .tar.gz files

Unpack tar.gz and .tgz archives from the command line: target folders, single files, stripped paths, ownership and safe handling of unknown archives.

Open

Back up data with rsync

Build dated, space-saving snapshot backups with rsync and hard links, pull them from a second machine and test restores.

Open

Xeon 6 servers

Up to 96 cores on one socket, from $522.51 a month.

Open

FAQ

Quick Answers

Tape archive. It was built to write file collections to tape and now creates archive files.

No. Tar calls gzip, bzip2, xz or zstd through flags such as -z, -j, -J and --zstd.

Use tar -rf on an uncompressed .tar file. Compressed archives cannot be appended in place.

Yes. GNU tar does it with --listed-incremental and a snapshot file.

Add --acls --xattrs --selinux when creating and extracting on Enterprise Linux images.