kmail.at
← learning

linux · difficulty ◆◆

tar — archive and compress files

Pack everything into one file, then unpack it anywhere.

tar was born to write data to magnetic tape in 1979. Half a century later it still ships the world's source code — and most people only ever learn two of its incantations.

2026-08-12 · 6 min read

$ tar

What it does

tar combines multiple files and directories into a single archive file, and can optionally compress it with gzip, bzip2, or xz. The name stands for 'tape archive' — it was originally written to back up data to magnetic tape.

Why it matters

tar is the default way Unix systems move and back up whole trees of files. Combined with a compressor, it shrinks a directory into one portable file you can copy, email, or ship to a server.

Example

$ tar -czf backup.tar.gz src/
(no output — archive created)
$ ls -lh backup.tar.gz
-rw-r--r-- 1 kmail kmail 4.2M Aug 12 09:00 backup.tar.gz

-c create, -z gzip, -f file. Packs src/ into backup.tar.gz.

$ tar -xzf backup.tar.gz
$ ls
src/  backup.tar.gz

-x extract, -z gzip, -f file. Restores the archive into the current directory.

Common flags

-c
create a new archive
-x
extract files from an archive
-z
filter through gzip (compress/extract .tar.gz)
-f
use an archive file (required before a filename)
-v
verbose — list each file as it is processed
-t
list the contents of an archive

History

Origin

tar was developed in 1979 at Bell Labs for backing up to tape drives, which is why its flags (c, x, f) read like tape operations. It predates the common gzip/jbzip compression, which were layered on later.

Fun fact

Despite its age, tar remains the standard for distributing software source code (the .tar.gz 'tarball') — and GNU tar is so foundational it ships on every Linux distribution by default.

Fun facts

Pros & cons

pros

  • + Preserves permissions, ownership, and directory structure exactly
  • + Universal — every Unix-like system ships it
  • + Compression is optional and swappable (-z, -j, -J)
  • + Streams, so you can pipe archives between machines without a temp file

cons

  • − Does not compress by itself; you must pair it with a compressor
  • − Classic flag syntax is easy to misremember
  • − Not ideal for incremental backups without extra tooling (--listed-incremental)
  • − A single corrupt block can hurt the whole archive (mitigate with gzip integrity checks)

Takeaways

  1. 1Create: `tar -czf archive.tar.gz dir/`
  2. 2Extract: `tar -xzf archive.tar.gz`
  3. 3Inspect: `tar -tzf archive.tar.gz` lists contents without extracting
  4. 4Pipe across the network: `tar -czf - dir/ | ssh host "cat > backup.tgz"`
  5. 5For incremental or encrypted backups, reach for restic or borg instead

Related commands

← all learning