Lesson 11 · tested on Debian 13
tar and gzip without the usual mistakes: back up a folder, skip the junk, verify the archive
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.
Video coming soon — @ShellProven
Commands
Exactly as run in the video. Full folder with setup script and log: terminal-tutorials/tar-gzip-backup.
# The project folder
$ 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
# Create an archive and look inside
$ tar -czf backup.tgz project
$ ls -lh backup.tgz
-rw-r--r-- 1 root root 8.1M Nov 21 20:16 backup.tgz
$ 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
# The trap: absolute paths
$ tar -czf abs.tgz /lab/work/project
tar: Removing leading `/' from member names
$ tar -tzf abs.tgz | head -3
lab/work/project/
lab/work/project/src/
lab/work/project/src/app.py
$ 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
$ tar -czf good.tgz -C /lab/work project && tar -tzf good.tgz | head -3
project/
project/src/
project/src/app.py
# Skip the junk with --exclude
$ 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
$ 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
# Verify the backup
$ gzip -t small.tgz && echo "gzip OK"
gzip OK
$ tar -dzf small.tgz -C /lab/work && echo "archive matches the disk"
archive matches the disk
$ 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
$ printf 'debug = false\n' > project/src/config.ini && tar -czf small.tgz --exclude=node_modules --exclude='*.log' -C /lab/work project
# Restore one 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
Quick check
Same two questions as in the video. Checked here, in your browser — nothing is sent anywhere.
Transparency. Script and narration made with AI, human-reviewed. The robot and the dog are AI-generated cartoon characters. The commands and their output are real runs in a container.