tar bundles files into one compressed archive. Learn the three shapes you actually use, `-czf` to create, `-tzf` to look inside and `-xzf` to extract, by running the commands in a real terminal in your browser.
Updated: 2026-09-06
tar [options] archive [file...]
It bundles files and directories into one file.
Pack src and logs together.
$ tar -czf backup.tar.gz src logs
Success is silent. Have a look at what you made.
$ ls -lh backup.tar.gz
-rw-r--r-- 1 user user 1.4K Sep 6 00:52 backup.tar.gz
Each letter of the option means one thing.
| Letter | Meaning |
|---|---|
c | create a new archive |
z | compress with gzip |
f | the file name comes next |
Swap c for t and you get the contents without extracting anything.
$ tar -tzf backup.tar.gz
src/
src/index.js
src/utils.js
logs/
logs/app.log
logs/error.log
Everything is wrapped in src/ and logs/, which is what you want to see.
Always look before you extract.
An archive whose contents are not inside a directory will spill its files straight into wherever you are standing.
Swap c for x to get the files back.
Here is what that looks like after deleting src by accident.
$ rm -r src
$ ls
backup.tar.gz logs/ README.md restore/
$ tar -xzf backup.tar.gz
$ ls src
index.js utils.js
Back again. Files land in the directory you are in.
-C picks the destination.
$ tar -xzf backup.tar.gz -C restore
$ ls restore
logs/ src/
That is the safe way to inspect an archive without touching your working copy.
Adding v for verbose prints each file as it goes.
$ tar -czvf logs.tar.gz logs
logs/
logs/app.log
logs/error.log
tar is what you type to move or keep a whole directory.
| Situation | What to type |
|---|---|
| Archive a month of logs | tar -czf logs-2026-09.tar.gz logs |
| Unpack something you downloaded | tar -xzf package.tar.gz |
| Check the contents first | tar -tzf package.tar.gz |
| Unpack without disturbing your work | tar -xzf package.tar.gz -C /tmp/check |
| Pack without the junk | tar -czf src.tar.gz --exclude=node_modules . |
Packing before you send something to a server, and unpacking what you received, cover most of it.
The file name follows f.
Write tar -cfz backup.tar.gz src and z gets read as the file name.
Learning -czf, -xzf and -tzf as fixed shapes avoids the whole problem.
The originals stay where they are.
tar writes a new archive and leaves the source alone, unlike gzip, which replaces the file it compresses.
A missing source stops it.
$ tar -czf docs.tar.gz nope
tar: nope: Cannot stat: No such file or directory
A missing archive says much the same.
$ tar -tzf nothing.tar.gz
tar: nothing.tar.gz: Cannot open: No such file or directorywebterm.appthis site
Advanced Terminal Commands
Learn commands for specific situations
learn.webterm.appa separate site

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.
See the course