tar bundles many files into one archive — originally for tape drives, today for backups, source drops, and release artefacts. With compression it is the .tar.gz you meet on every download page.

The two commands everyone memorises #

Create and extract a compressed archive
$ tar -czf project.tar.gz project/
$ tar -xzf project.tar.gz

Read the letters as a sentence: c create, z gzip-compress, f filename; x extract. The order of the flags does not matter; what matters is choosing c or x, and that f comes last before the filename.

The full flag set #

FlagMeaning
-cCreate an archive
-xExtract an archive
-tList contents without extracting
-zFilter through gzip (.tar.gz); -j bzip2, -J xz
-fThe archive filename, always last
-vVerbose: list files as they are processed
-C dirChange to dir before acting
--exclude=PSkip paths matching a pattern

Verify before you extract #

List the contents of an archive
$ tar -tzf project.tar.gz | head

One habit prevents the classic mistake — an archive that explodes 40 files into your current directory: list first. If the entries lack a leading folder, extract into a new one:

Extract into a directory you control
$ mkdir restore && tar -xzf project.tar.gz -C restore

Backups with excludes #

Back up a project, skipping caches
$ tar -czf backup.tar.gz --exclude="node_modules" --exclude=".git" project/

Modern GNU tar detects compression from the filename, so tar -xf anything.tar.gz works too — but writing the z explicitly costs nothing and works on older systems.

To move an archive to another machine, pair it with ssh; to publish or fetch one, curl uploads and downloads it. More in the journal.