$_ CLI In Depth
Learning progress 0%
lesson disks mounts archives

Archiving with tar

5 min read Module 12: Disks, Mounts & Archives Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

What an archive is

A tar archive is a sequence of entries. Each entry starts with a header that records the name, the file type, the permissions, the owner, the size, and the modification time. The data for regular files follows the header, padded to a fixed block size. Directories and symbolic links have headers but no data. Because the whole tree is one stream, you can copy it, compress it, send it over a network, or store it on tape without losing the structure.

The format is simple on purpose. It does not compress anything, it does not index the contents, and it does not check integrity. Those jobs belong to other tools that work on the stream, and later lessons in this module use gzip, xz, and sha256sum for them.

The three modes

Every tar command needs one mode option. -c creates an archive from the paths you name. -t lists the entries in an archive without extracting them. -x extracts entries into the current directory or the directory you choose with -C. The -f option names the archive file, and -v prints each name as it is processed. A common mistake is leaving out -f, in which case tar reads from or writes to a default device.

Create a small tree and archive it. The learner in the lab creates files with plain redirection, so every step can be repeated exactly.

BASH4 lines
mkdir -p project
echo alpha > project/a.txt
echo beta > project/b.txt
tar -cf project.tar project

The archive contains the directory project/ and both files. Notice that the paths in the archive are the paths you gave to tar. If you pass a relative path, the archive stores a relative path, and extraction recreates the same relative tree.

Listing before you extract

Listing an archive first is the safest habit. It shows what the archive will write, and it reveals absolute paths or unexpected top-level names before any file is created. Add -v to see the modes, owners, sizes, and dates as well.

BASH2 lines
tar -tf project.tar
tar -tvf project.tar

The first form prints names only. The second prints a long listing like ls -l. Both read the archive without touching the disk tree, so they are the correct way to check an unknown archive from the internet.

Warning

An archive can contain absolute paths or ../ entries. Modern GNU tar strips leading slashes and refuses to write outside the target directory by default, but a list command is still the way to be sure before you extract.

Extracting into a directory

Extraction writes files relative to the current directory unless you choose another with -C. Using -C keeps a restore separate from the original tree, which is what you want when you are testing a backup.

BASH3 lines
mkdir -p restore
tar -xf project.tar -C restore
diff -r project restore/project && echo same

The diff -r command compares the two trees recursively. When it prints nothing and the message same appears, the restored tree matches the original byte for byte, names and directory structure included. A restore that you have not compared is a guess.

Selecting members

You can add only some entries to an archive. The -C option also works on create, and it changes directory before reading the next name. The archive then stores names relative to that directory, which keeps paths short and predictable.

BASH2 lines
tar -cf only-a.tar -C project a.txt
tar -tf only-a.tar

The listing shows only a.txt. The name in the archive has no project/ prefix, because the directory change happened before the entry was read. This is the form you will use in backup scripts that gather files from several places.

Common mistakes with paths

Most restore problems come from paths, not from tar itself. An archive that stores project/a.txt extracts under whatever directory you name with -C, so a restore into the wrong directory creates a second copy without any error. Listing the archive first shows the stored names, so you can decide where they belong before anything is written.

Absolute paths are the second source of surprises. When you archive a path that starts with a slash, GNU tar removes the leading slash by default, so the stored names are relative and extraction writes under the directory you choose. Keeping the slash would write over the live system when you restore, which is why the default is safe and why you should not override it casually.

The third mistake is archiving a directory that is still changing. Tar reads files one at a time, so a file that changes during the run can be captured in a mixed state. For a consistent copy, stop the writer or copy from a snapshot, and archive the snapshot.

Reading one member without extracting

Two forms are useful for peeking at an archive. The first lists entries with a pattern, which narrows the output to the names you care about. The second extracts one file to standard output, so you can read a single member without creating anything on disk.

BASH2 lines
tar -tf project.tar '*/a.txt'
tar -xOf project.tar project/a.txt

The -O option sends the extracted data to standard output, so the second command prints alpha and creates no file. Both forms leave the working directory untouched, so they are safe to use on an archive you have not yet decided to trust. Extraction comes only after the listing looks right.

Hands-on: create, list, and restore

Combine the three modes. Create an archive of the project directory, list it, and restore it into a new directory. The final comparison is the test that the archive is complete.

BASH5 lines
tar -cf backup.tar project
tar -tf backup.tar
mkdir check
tar -xf backup.tar -C check
diff -r project check/project && echo restored

If the comparison prints the word restored, the archive contains everything the original tree had. Keep this three step rhythm, create then list then restore and compare, for any backup you rely on.

Inside a tar archive, and what gzip wraps

Visualizer

A tar file is a run of 512-byte blocks: a header, then the data padded to 512 bytes, and two zero blocks at the end. Compression wraps the whole stream, not each file.

1 / 5
The archive, in 512-byte blocks
0header block: name, mode, size, mtime, checksum
1data block: hi and newline, padded with zeros to 512
2zero block
3zero block: end of archive

A member is a 512-byte header followed by its data. The data is padded with zero bytes to a multiple of 512, so the 3-byte file still occupies one whole block.

printf 'hi\n' > /tmp/h.txt && tar -cf /tmp/h.tar -C /tmp h.txt && wc -c < /tmp/h.tar && od -A d -c -j 512 -N 3 /tmp/h.tar
Terminal lab · 3 steps · real Linux in your browser

Create, subset, and restore an archive

Build a small tree, archive it, extract it into a new directory, and archive one member.

01
Create the project archive
Make a directory named project with two files, a.txt containing alpha and b.txt containing beta. Then archive the whole directory into project.tar with tar -cf. ``bash mkdir -p project echo alpha > project/a.txt echo beta > project/b.txt tar -cf project.tar project ``
Show hint for step 1
The -f option names the archive, and the archive must contain project/ and both files.
02
Restore into a new directory
Extract project.tar into a new directory named restore with -C. Then compare the restored tree with the original using diff -r. No output from diff means the trees match. ``bash mkdir -p restore tar -xf project.tar -C restore diff -r project restore/project && echo same ``
Show hint for step 2
-C chooses the extraction directory. diff -r compares names and contents.
03
Archive one member
Create only-a.tar that contains a.txt alone. Use -C project so the stored name has no directory prefix, then list the archive to confirm. ``bash tar -cf only-a.tar -C project a.txt tar -tf only-a.tar ``
Show hint for step 3
Put -C project before the file name a.txt. The listing should print only a.txt.
Check yourself Question 1 of 4
Concept Check The archive name option recall

Which tar option names the archive file that tar reads or writes?

Recap & Key Takeaways

A tar archive is one stream of headers and data that stores a tree's names, types, modes, and contents, and it does not compress or checksum anything by itself.
Use -c to create, -t to list, and -x to extract, and always name the archive with -f.
List an archive with tar -tf before extracting it, and use -C to choose the directory that receives the files.
Verify a restore with diff -r against the original tree, because a successful extract does not prove the contents match.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue