tar basics: create, extract, and verify archives
tar is the classic place to start on Linux when you want to move, copy, or back up data. But most people learn it as “a one-liner they happen to have in their notes”. This is the basics: the simple, foundational pieces everything else builds on. The written version of the tar basics video series — what the commands do, why, and how to prove the result actually works.
One file in, one file out
The simplest thing tar can do, proven end to end: put one file into an archive, take the same file back out — a full round-trip.
mkdir -p /tmp/pilot && echo 'hello world' > /tmp/pilot/notes.txt
tar -czf /tmp/pilot/notes.tar.gz -C /tmp/pilot notes.txt
mkdir -p /tmp/pilot/restored
tar -xzf /tmp/pilot/notes.tar.gz -C /tmp/pilot/restored
cat /tmp/pilot/restored/notes.txt # -> hello world
Extracting into a separate directory is the whole point: the archive holds a copy, so unpacking it somewhere else and getting the file back proves the round-trip really works.
The -C flag deserves the credit: it changes directory first, so the
archive stores notes.txt — not a house of nested paths you have to climb
through to restore.
The flags, decoded
Stop memorizing one-liners. Each letter is one job:
-c— create an archive-x— extract files-f— the file (path) to read or write-t— list the contents without extracting-v— verbose: name each file as it is handled-z— gzip compression (a separate layer on top)
A small archive to inspect:
mkdir -p /tmp/pilot2 && echo data > /tmp/pilot2/a.txt
tar -czf /tmp/pilot2/out.tar.gz -C /tmp/pilot2 a.txt
tar -tf /tmp/pilot2/out.tar.gz # list contents
gzip -t /tmp/pilot2/out.tar.gz && echo OK # compression stream OK
tar -tzvf /tmp/pilot2/out.tar.gz # verbose listing
-t lists without touching the filesystem; gzip -t checks the
compression layer, which is separate from tar’s own format. -v turns
every listing into per-file detail.
A backup you can prove
A backup you haven’t tested isn’t a backup. Archive a project, skip a junk cache folder on purpose, restore elsewhere, then prove the restore matches the original:
mkdir -p /tmp/project/{src,cache} && echo code > /tmp/project/src/app.py
echo junk > /tmp/project/cache/tmp.bin
tar -czf /tmp/project-backup.tar.gz --exclude=cache -C /tmp/project .
mkdir -p /tmp/restored-project
tar -xzf /tmp/project-backup.tar.gz -C /tmp/restored-project
diff -r /tmp/project/src /tmp/restored-project/src && echo RESTORE-OK
--exclude=cache leaves the junk outside the archive; diff -r is the
real test. Byte-for-byte identical — or it didn’t work.
Watch the videos:
- Part 1 — create and extract: How to create and extract a .tar.gz archive
- Part 2 — the flags: What the tar flags actually mean (c x f t v z)
- Part 3 — backup + verify: Back up a folder with tar and prove the restore is identical