Linux command list

tar command: pack a directory into a single file

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying tar. Nothing here can touch your real files.
user@webterm:~/project$
 

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

Syntax

tar [options] archive [file...]

It bundles files and directories into one file.

Try it first

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.

LetterMeaning
ccreate a new archive
zcompress with gzip
fthe file name comes next

Looking inside

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.

Extracting

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.

Extracting somewhere else

-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.

Watching it work

Adding v for verbose prints each file as it goes.

$ tar -czvf logs.tar.gz logs
logs/
logs/app.log
logs/error.log

When you actually reach for it

tar is what you type to move or keep a whole directory.

SituationWhat to type
Archive a month of logstar -czf logs-2026-09.tar.gz logs
Unpack something you downloadedtar -xzf package.tar.gz
Check the contents firsttar -tzf package.tar.gz
Unpack without disturbing your worktar -xzf package.tar.gz -C /tmp/check
Pack without the junktar -czf src.tar.gz --exclude=node_modules .

Packing before you send something to a server, and unpacking what you received, cover most of it.

Things that trip people up

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 directory

Practise it hands-on

webterm.appthis site

  • Advanced Terminal Commands

    Learn commands for specific situations

    Try the tutorial

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

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

Related commands

gzip / zip / ls / du

Frequently asked questions

What do czf and xzf stand for?
`c` is create, `x` is extract, `t` lists the contents, `z` means gzip, and `f` says the next word is the file name. Rather than memorising combinations, think of it as picking one of `c` / `x` / `t` and adding `zf`.
What is the difference between .tar and .tar.gz?
A `.tar` is only bundled; a `.tar.gz` is that bundle compressed with gzip. Adding `z` compresses it, and the `.tar.gz` name is the convention that goes with it.
Where do the files end up when I extract?
In the directory you are standing in. Use `-C <directory>` to send them elsewhere, and look inside with `-tzf` first so nothing gets scattered.
Where does -f go?
Last among the options, because the word right after it is read as the file name: `tar -czf backup.tar.gz src`.
Should I add -v?
It prints one line per file, which is reassuring on a big directory. Most of the time you do not need it.