The filesystem and archives

INPUT · Slides

Pack it all into one (`tar`)

01 / 06

A hundred files into one

Handing files to someone, nobody hands over a hundred loose ones. You put them in a box.

The tool that does that is tar.

docs/              tar cf docs.tar docs|- a.txt      ->   docs.tar (one file)\- inner/   \- b.txt

The name comes from tape archive. Long ago it was the tool for taking backups onto tape. Nobody uses tape now, and the name and the tool have stayed.

What matters is that tar is not a tool for compressing.

tar    packs it up (the size hardly changes)gzip   shrinks it (next lesson)

"Packing" and "shrinking" are separate jobs. Linux combines small tools, so these two are separate too.

And the great strength of tar is that it keeps the shape of the directories. The nesting, the permissions and the owners all go into the box together.

02 / 06

Three jobs, three letters

tar does only three things. Learn them one letter at a time.

LetterMeaningThe word
cmake onecreate
tsee insidelist
xtake things outextract

To that you add f (file), saying which file it is. That is the basic shape.

tar cf box-name what-to-put-in     make onetar tf box-name                    see insidetar xf box-name                    take things out

Typed out, it goes like this.

~ $ tar cf docs.tar docs~ $ tar tf docs.tardocs/docs/inner/docs/inner/b.txtdocs/a.txt~ $ tar xf docs.tar

tar is an old tool, so the marks work without a hyphen.

tar cf ...     <- the old waytar -cf ...    <- this works too

You meet both, so be able to read both. This course writes them without the hyphen.

03 / 06

Do not get c and x the wrong way round

Look at the frightening mistake first.

tar cf docs.tar docs    <- right (make the box)tar cf docs docs.tar    <- disaster!

The second line means "make a box named docs and put docs.tar into it". The original directory is overwritten by the box and gone.

Remember it this way.

tar cf  [the box]  [what goes in]         ^ the box name first

What comes straight after the f is always the box, because the f of -f means "the next thing is the file name".

And one more. Extracting overwrites in silence.

~ $ echo edit > docs/a.txt   (rewritten)~ $ tar xf docs.tar          (extracted)~ $ cat docs/a.txtaaa                          (back as it was!)

When you do not want it overwritten, add k (keep).

tar xkf docs.tar     leave what is already there

The same property as overwriting with >. Before an operation you cannot undo, look inside with tf. Make that a habit.

04 / 06

Change place with -C

When you want to choose where things come out, there is -C (Change directory).

~ $ mkdir restore~ $ tar xf docs.tar -C restore

Out they come inside restore. Without this you end up doing the awkward thing of cd-ing and pointing at the box with a relative path.

cd restore && tar xf ../docs.tar    <- the same, the long way round

-C works when making one too, which is the slightly interesting part.

~ $ tar cf contents.tar -C docs .~ $ tar tf contents.tar././inner/./inner/b.txt./a.txt

No docs/ level on top: only the contents went in.

tar cf x.tar docs        ->  they go in as docs/a.txttar cf x.tar -C docs .   ->  they go in as ./a.txt

The difference shows when you extract, in whether there is one more level. A box you hand to somebody is better as the first kind (with the directory), because files do not scatter across the place the moment it is opened.

05 / 06

Look inside, take out just one

t is the mark for seeing inside without opening the box. Add v (verbose) for detail.

~ $ tar tvf docs.tardrwxr-xr-x 100/101   0 2026-08-22 00:31 docs/-rw-r--r-- 100/101   4 2026-08-22 00:31 docs/a.txt

Permissions, owners and dates are all in there. That is what "tar keeps the shape" means.

Write a name after it and just that one comes out.

~ $ tar xf docs.tar docs/a.txt

You use this when you want one file back out of a huge backup. Write the name exactly as tf prints it.

To put it on the screen alone, there is O (a capital O, Output to stdout).

~ $ tar xOf docs.tar docs/a.txtaaa

Seeing the contents without making a file means you can join it to grep.

tar xOf log.tar var/log/messages | grep error

There is also --exclude for leaving things out.

tar cf without.tar --exclude 'inner' docs

That packs it with inner left out entirely.

06 / 06

Now have a go

Here are the shapes for the lesson.

tar cf box.tar source        pack ittar tf box.tar               see insidetar tvf box.tar              see it in detailtar xf box.tar               take it outtar xf box.tar -C somewhere  take it out somewhere chosentar xf box.tar name          take out just onetar xOf box.tar name         only put it on the screentar xkf box.tar              do not overwrite what is theretar cf box.tar --exclude pattern source    pack it, leaving that out

The rule about order, once more.

straight after the f is always the box name

Keep to that and no accident happens.

One thing worth knowing. The tar in this environment cannot use the z mark (gzip compression).

tar czf x.tar.gz docs   ->  unrecognized option: z

So here you split packing and shrinking into two moves.

tar cf x.tar docs      pack itgzip x.tar             shrink it -> x.tar.gz

On a real Linux tar czf does both in one, so keep that shape in mind too. Compression itself gets a proper lesson next time. Now, make a box.