tar
37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0tar (“tape archive”) packs a directory tree into one stream, keeping names, permissions, owners, timestamps, symlinks and (optionally) ACLs and extended attributes. It does not compress; compression is a separate layer that tar runs for you, giving .tar.gz, .tar.xz, .tar.zst and so on. Combined with a good compressor, tar is the right tool for Unix backups and source distribution, and the compressor sees the whole archive as one stream, so it can exploit redundancy across files, unlike ZIP.
Common commands
Section titled “Common commands”tar -cf archive.tar dir/ # createtar -tf archive.tar # listtar -xf archive.tar -C out/ # extract (into out/, which must exist)tar -xf archive.tar path/in/archive # extract one memberReading is automatic: tar -xf detects gzip, xz, bzip2, zstd and others by content. For creating, pick the compressor by flag or suffix:
| Flag | Compression |
|---|---|
-z | gzip |
-J | xz |
-j | bzip2 |
--zstd | zstd |
-a (--auto-compress) | chosen from the archive suffix: tar -caf out.tar.zst dir/ |
-I 'prog args' | any program: tar -I 'zstd -19 -T0' -cf out.tar.zst dir/ |
Use -I for options such as threads and levels; -I pigz gives parallel gzip. For compressors tar does not know, such as brotli, pass -I for extraction as well: tar -I brotli -xf out.tar.br.
Backups and restores
Section titled “Backups and restores”To preserve everything you will need on restore:
tar --xattrs --acls --numeric-owner -I 'zstd -T0' -cpf backup.tar.zst /path/to/dirsudo tar --xattrs --acls --numeric-owner -xpf backup.tar.zst -C /restore-p keeps permissions exactly (extraction as root does this by default). Owner restore only works as root, and --numeric-owner restores by UID/GID instead of names, which matters when restoring on a different system. --one-file-system stops tar crossing mount points, and --exclude='pattern' skips paths.
Reproducible archives
Section titled “Reproducible archives”Tar embeds timestamps, ordering and owner names. To get identical bytes from identical content:
tar --sort=name --mtime='2026-01-01 00:00Z' --owner=0 --group=0 --numeric-owner -cf - dir/ | zstd -T0 > dir.tar.zstFormats and tools
Section titled “Formats and tools”GNU tar writes the GNU format by default. --format=pax (POSIX.1-2001) supports long paths and sub-second times without the GNU extensions, and --format=ustar is the oldest and most limited. bsdtar (libarchive, the default on macOS and Windows) reads all of them. Because tar archives are a plain stream, there is no index: listing or extracting one file means reading through the archive, and a damaged stream loses everything after the damage unless the compressor limits it.
Pitfalls
Section titled “Pitfalls”- Absolute paths: GNU tar strips the leading
/on creation, sotar -xfwill not overwrite system files. Use-C /for a full restore. - Compression flags and
-f:-ftakes the next argument, sotar -cfz out.tgz dircreates a file namedz. Put-flast (-czf out.tgz). - Symlinks and hard links are stored as links; use
-hto follow symlinks instead.