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 #
$ 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 #
| Flag | Meaning |
|---|---|
-c | Create an archive |
-x | Extract an archive |
-t | List contents without extracting |
-z | Filter through gzip (.tar.gz); -j bzip2, -J xz |
-f | The archive filename, always last |
-v | Verbose: list files as they are processed |
-C dir | Change to dir before acting |
--exclude=P | Skip paths matching a pattern |
Verify before you extract #
$ 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:
$ mkdir restore && tar -xzf project.tar.gz -C restore
Backups with excludes #
$ 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.
