DEV Community

tar and gzip without the usual mistakes: back up a folder, skip the junk, verify the archive

AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (GNU tar 1.35, gzip 1.13); all outputs below are copied from that run, not written by hand.

Back up a folder with tar and gzip the right way: create and list an archive, see why an absolute path ends up nested three folders deep when you extract it (and fix it with -C), skip node_modules and log files with --exclude, prove the archive matches the disk with tar --compare, and restore a single file.

The problem

You need a quick backup of a folder before you change something. tar can do it in one line, but most people only find its traps when they try to restore.

The project folder

Here is the folder we want to back up. A little source code, a big node_modules folder, and a log file.

$ find project -type f | sort
project/README.md
project/logs/app.log
project/node_modules/lib/bundle.bin
project/src/app.py
project/src/config.ini
$ du -sh project
8.1M project

About eight megabytes, and almost all of it is stuff we could download or regenerate.

Create an archive and look inside

tar -czf means: create, compress with gzip, and write to the file named next. The file name must come right after the f.

$ tar -czf backup.tgz project
$ ls -lh backup.tgz
-rw-r--r-- 1 root root 8.1M Nov 21 20:16 backup.tgz

Before you trust an archive, list it. -t shows what's inside without extracting anything.

$ tar -tzf backup.tgz
project/
project/src/
project/src/app.py
project/src/config.ini
project/node_modules/
project/node_modules/lib/
project/node_modules/lib/bundle.bin
project/logs/
project/logs/app.log
project/README.md

Every path starts with project/. That's what you want: extract it anywhere, and you get one project folder.

The trap: absolute paths

Now the mistake almost everyone makes once. We give tar the full path to the folder.

$ tar -czf abs.tgz /lab/work/project
tar: Removing leading `/' from member names

Tar warns that it removed the leading slash, and most people ignore that line. Let's see what it means.

$ tar -tzf abs.tgz | head -3
lab/work/project/
lab/work/project/src/
lab/work/project/src/app.py

The archive now contains the whole path: lab, work, project. Extract it into a new folder and see what you get.

$ mkdir /tmp/restore && tar -xzf abs.tgz -C /tmp/restore && find /tmp/restore -maxdepth 3 -type d
/tmp/restore
/tmp/restore/lab
/tmp/restore/lab/work
/tmp/restore/lab/work/project

Your files are three folders deep. It's safe, nothing outside the target was touched, but it's rarely what you wanted.

The fix is -C: change into the parent folder first, then name only the folder you want to keep.

$ tar -czf good.tgz -C /lab/work project && tar -tzf good.tgz | head -3
project/
project/src/
project/src/app.py

Short, relative paths again, no matter where you run the command from.

Skip the junk with --exclude

The backup doesn't need node_modules or old logs. --exclude takes a pattern. Put the options before the folder name.

$ tar -czf small.tgz --exclude=node_modules --exclude='*.log' -C /lab/work project
$ tar -tzf small.tgz
project/
project/src/
project/src/app.py
project/src/config.ini
project/logs/
project/README.md

Keep the quotes around *.log, so the shell doesn't expand it before tar sees it. Now compare the sizes.

$ ls -lh good.tgz small.tgz
-rw-r--r-- 1 root root 8.1M Nov 21 20:16 good.tgz
-rw-r--r-- 1 root root 282 Nov 21 20:16 small.tgz

From eight megabytes down to a few hundred bytes. The things you skipped can always be rebuilt.

Verify the backup

A backup you never checked is just a hope. First, gzip can test that the file isn't damaged.

$ gzip -t small.tgz && echo "gzip OK"
gzip OK

Better still, tar --compare, or -d, checks every file in the archive against the disk. No output means they match.

$ tar -dzf small.tgz -C /lab/work && echo "archive matches the disk"
archive matches the disk

Now let's change a file after the backup, and compare again.

$ echo "debug = true" > project/src/config.ini && tar -dzf small.tgz -C /lab/work
project/src/config.ini: Mod time differs
project/src/config.ini: Size differs

Tar names the file and says what differs. Let me put the original content back.

$ printf 'debug = false\n' > project/src/config.ini && tar -czf small.tgz --exclude=node_modules --exclude='*.log' -C /lab/work project

Restore one file

You rarely need everything back. Give tar the path exactly as it appears in the listing, and it extracts only that file.

$ mkdir /tmp/one && tar -xzf small.tgz -C /tmp/one project/src/config.ini && find /tmp/one -type f
/tmp/one/project/src/config.ini

Extract into an empty folder like this, then copy what you need. Extracting straight over your project would overwrite newer files without asking.

The trap, in one paragraph

How can I prove an archive still matches the files on disk? gzip -t only checks the compression. tar -d compares every file, and prints nothing when they match.

Quick check

  1. I archived /lab/work/project by its full path and extracted it in /tmp/restore. Where are my files?

    • /tmp/restore/project
    • /tmp/restore/lab/work/project
    • Back in /lab/work/project

    Answer /tmp/restore/lab/work/project: Tar keeps the whole path, minus the leading slash. Use -C with the parent folder to get short paths.

  2. How can I prove an archive still matches the files on disk?

    • gzip -t small.tgz
    • tar -dzf small.tgz -C /lab/work
    • ls -lh small.tgz

    Answer tar -dzf small.tgz -C /lab/work: gzip -t only checks the compression. tar -d compares every file, and prints nothing when they match.

Recap

tar -czf creates, tar -tzf lists, and tar -xzf extracts, ideally into an empty folder with -C. Archive with -C and a relative name to avoid nested paths, skip junk with --exclude, and check your backup with tar -d.

Source and reproducible setup

This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.