Introduction
Tar bundles files and folders into one file, keeping each file’s name, permissions, owner and modification time. It began in Version 7 Unix in 1979 as a way to write files to magnetic tape, which is why everything in it comes in 512-byte blocks. POSIX.1-1988 standardised it as the ustar format, and POSIX.1-2001 extended it as the pax format.
Tar does not compress. A tar file is usually compressed as a whole afterwards, giving
.tar.gz or .tgz (gzip),
.tar.xz (xz),
.tar.bz2 (bzip2) and
.tar.zst (Zstandard). Tar is also found
inside other formats: Debian packages hold their files in tar archives, and container
images are made of tar layers.
Structure
A tar file is a sequence of 512-byte blocks. Each file in the archive is a header block describing it, followed by its contents, padded with zeros to a whole number of blocks. A folder, a link or a device is a header with no contents; the header’s type flag says which it is. Two blocks of zeros end the archive.
0Header name, mode, owner, size, time, checksum, type512 bytes512File data the file’s bytes, padded with zeros to a multiple of 512size rounded up…Next header and its data, for each further filerepeated…End of archive two blocks of zeros, then padding to a whole record1024 bytes or more
There is no index. To list an archive, a reader reads each header, uses the size in it to skip over the file’s data, and reads the next header. Long names, large files and other details the original header cannot hold are carried in extra headers before the file’s own: the pax extensions of POSIX, or the older GNU extensions.
| Variant | Bytes 257 to 264 | Notes |
|---|---|---|
| Version 7 | zeros | the original 1979 header, with no owner names and no prefix |
| ustar | ustar\0 00 | POSIX.1-1988: owner names, device numbers and a 155-byte name prefix |
| GNU | ustar and two spaces and \0 | GNU tar’s own layout, with long names and sparse files |
| pax | ustar\0 00 | ustar headers plus extended headers of key and value records |
Links
Software
Developers
- libarchive, C library that reads and writes every tar variant
- tarfile, Python standard library module
- archive/tar, Go standard library
Reference
- pax, the POSIX description of the ustar and pax formats
- Basic Tar Format, GNU tar manual