The tar command in Linux
tar (tape archive) bundles files and directories into a single archive file, usually compressed with gzip (.tar.gz / .tgz). It is the standard way software, backups and datasets are distributed on Linux. Everyone forgets its flags; the two you actually need are -czf to create and -xzf to extract — think "Create Zip File" and "eXtract Zip File".
How tar works
A tar archive is the simplest format that could work: a linear stream of 512-byte blocks — for each member, a header block (name, size, mode, owner, timestamps) followed by its data, then the next member, end to end. No index, no central directory. That streamability is the point: tar can be produced on the fly and consumed on the fly, which is why it pipes through ssh and gzip so naturally.
Compression is deliberately not tar's job. The archive stream is handed whole to gzip, bzip2 or xz — that is all -z, -j and -J select — so .tar.gz compresses across file boundaries (better ratios on many small files than zip's per-file compression). The trade-off of having no index: listing or extracting one file requires reading through the stream from the start. Archive formats are the file-format equivalent of pipes, and tar chose the pipe side of every trade.
Syntax
tar [OPTIONS] [ARCHIVE] [FILES...] Common options
| Option | What it does |
|---|---|
-c | Create a new archive. |
-x | Extract an archive. |
-t | List the contents without extracting. |
-z | Compress/decompress with gzip (.tar.gz). |
-j | Compress/decompress with bzip2 (.tar.bz2). |
-f FILE | The archive file name. Must come last in the flag group, right before the name. |
-v | Verbose: list files as they are processed. |
-C DIR | Change to DIR before extracting (extract somewhere else). |
How to use tar: examples
$ tar -czf backup.tar.gz documents/ Create a gzip-compressed archive of the documents directory.
$ tar -xzf backup.tar.gz Extract it in the current directory. This is the command you will Google most in your life — memorize it now.
$ tar -tzf backup.tar.gz Peek inside the archive without extracting anything.
$ tar -xzf app.tar.gz -C /opt/app Extract into a specific directory instead of the current one.
$ tar -czf logs.tar.gz /var/log --exclude="*.gz" Archive a directory but skip files matching a pattern.
$ tar -xzf big.tar.gz path/inside/file.txt Extract a single file from an archive.
Real-world use cases for tar
Server-to-server migration
Moving a site between machines: tar -czf - /var/www | ssh user@new "tar -xzf - -C /" streams the archive through SSH without ever writing a temporary file — compression, transfer and extraction in one pipeline. The canonical Unix migration one-liner.
Dated backups
tar -czf "backup-$(date +%F).tar.gz" ~/projects produces backup-2026-08-04.tar.gz — self-labeling archives. Add an --exclude for node_modules and you have the minimal viable backup script most freelancers actually run.
Pro tips and common mistakes
- Modern tar auto-detects compression on extraction: tar -xf works for .tar.gz, .tar.bz2 and .tar.xz alike — the z is only mandatory when creating.
- Always list before extracting archives from the internet: tar -tf pkg.tar.gz | head. Well-behaved archives contain one top-level directory; "tar bombs" spray files into your current dir.
- Exclusions must be quoted and repeated: tar -czf site.tar.gz . --exclude="./node_modules" --exclude="./.git".
- For simple compress-this-directory jobs, zip -r out.zip dir may serve better if the recipient is on Windows.
Frequently asked questions about tar
How do I extract a .tar.gz file?
tar -xzf filename.tar.gz — x extracts, z handles the gzip compression, f names the file. Add -v to watch the files come out.
What is the difference between .tar, .tar.gz and .zip?
A .tar bundles files without compressing; .tar.gz is a tar archive compressed with gzip. Unlike zip, which compresses each file separately, tar.gz compresses the whole stream, usually achieving better ratios on many small files.
Why does the order of tar flags matter?
Mostly it doesn't, with one exception: -f must be immediately followed by the archive name, so it goes last in a combined group (-czf archive.tar.gz, never -cfz).
Related commands
Copy files and whole directory trees, preserving attributes when needed.
findLocate files anywhere in a directory tree by name, type, size, date — and act on them.