Intermediate · Automation basics: scheduling, archives and shell scripts · topic 36 of 57 · 30 min
Archives and compression: tar, gzip, bzip2 and xz
Explanation
Archive versus compress
An archive joins many files and folders into one file, keeping names, owners, permissions and times; tar (tape archive) does this. Compression makes a file smaller by encoding repeated patterns; gzip, bzip2 and xz do this, one file at a time. That is why you so often see .tar.gz: first tar joins, then gzip shrinks.
The tar options you need
| Option | Meaning |
|---|---|
| -c | create an archive |
| -x | extract |
| -t | list the contents |
| -f FILE | the archive file name (put it right after f) |
| -v | verbose: show each file |
| -z / -j / -J | gzip (.tar.gz, .tgz) / bzip2 (.tar.bz2) / xz (.tar.xz) |
| -C DIR | change to DIR first (where to extract, or where to read from) |
| -p | keep permissions when extracting (default for root) |
| --selinux, --acls, --xattrs | also save SELinux labels, ACLs and extended attributes |
# tar -czf /backup/etc-2026-10-19.tar.gz /etc
tar: Removing leading `/' from member names
# tar -tzf /backup/etc-2026-10-19.tar.gz | head -3
etc/
etc/mtab
etc/fstab
# mkdir /tmp/restore
# tar -xzf /backup/etc-2026-10-19.tar.gz -C /tmp/restore etc/fstab # one file only
# ls /tmp/restore/etc
fstab
# tar -cJf home.tar.xz -C /home asha ravi # paths relative to /home
# tar -xf home.tar.xz -C /srv/restore # modern tar detects the compressionChoosing a compressor
| Tool | Extension | Speed | Size |
|---|---|---|---|
| gzip | .gz | Fastest | Biggest of the three |
| bzip2 | .bz2 | Slower | Smaller |
| xz | .xz | Slowest to compress | Usually smallest |
$ gzip access.log # becomes access.log.gz (the original is replaced)
$ gunzip access.log.gz # back
$ gzip -k access.log # -k keeps the original
$ xz -9 big.sql # -9 = best compression, slow
$ zcat access.log.gz | grep 503 # read a .gz without unpacking (also xzcat, bzcat)
$ ls -lh
-rw-r--r--. 1 root root 86M Oct 19 16:01 big.sql.xzText such as logs and SQL dumps often compresses to a tenth of its size or less. Photos, videos and already-compressed files hardly shrink at all; do not waste CPU compressing them again.
Copying archives between servers
Archives are usually copied to another machine for safety. scp file user@host:/path copies over SSH; rsync -av copies only what changed, which is ideal for repeated backups. A backup kept only on the same disk is not a backup.
Common mistakes
- tar -cfz archive.tar.gz ...: the word after -f is taken as the archive name, so this creates a file named z. Keep f last: -czf.
- Extracting in the wrong directory and spreading files everywhere, or over live files. List with -t first and use -C to a fresh directory.
- Backing up /var/www without --selinux or --acls, then restoring and getting permission errors. For system files, add --selinux --acls --xattrs or run restorecon after restore.
- Never testing a restore. A backup you have not restored once is a hope, not a backup.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Archives and compression: tar, gzip, bzip2 and xz · 1/7
tar joins many files into one archive. gzip, bzip2 and xz make files smaller.
Prefer a video by a teacher? Find free videos on “linux tar gzip bzip2 xz compression tutorial” on YouTube ↗
In real life: Shipping logs to the audit team at a broking firm
SEBI rules require a Mumbai stock-broking firm to keep trading-server logs for years. Every night, a script tars and xz-compresses the previous day's logs, which shrinks 4 GB to about 300 MB, and copies the archive to a separate storage server with rsync. Once a month someone restores a random archive to check it can be read.
# tar -cJf /archive/trade-logs-2026-10-18.tar.xz -C /var/log/trading 2026-10-18
# ls -lh /archive/trade-logs-2026-10-18.tar.xz
-rw-r--r--. 1 root root 297M Oct 19 00:42 /archive/trade-logs-2026-10-18.tar.xz
# rsync -av /archive/ backup@store01:/archive/trading/Practice (0/4 done)
Which command creates a bzip2-compressed archive of /home?
Which line has the bug? 1: mkdir -p /restore 2: tar -xzf -C /restore site.tar.gz
A log grows by daily_mb every day. Logs are rotated daily, the current day stays uncompressed, and keep_days older days are kept compressed to ratio (0.1 means 10% of the size). Write rotation_space(daily_mb, keep_days, ratio) that returns the total MB used, rounded to 1 decimal: daily_mb + keep_days * daily_mb * ratio.
Back up /etc into /root/backups/etc-<date>.tar.gz keeping SELinux labels and ACLs, list it, restore only /etc/hosts into /tmp/restore, and compare it with the real file.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Bash scripts: variables, quoting, arguments and exit codesAutomation basics: scheduling, archives and shell scripts · same module
- Scheduling with cron and crontabAutomation basics: scheduling, archives and shell scripts · same module
- Masks and flagsC Programming (from zero to systems) · Bits, undefined behaviour and safety
- Azure compute and storageCloud Computing (AWS, Azure, Google Cloud) · Microsoft Azure essentials
- Building for productionFrontend Web Development · Build, deploy and get hired
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which tar option lists an archive without extracting?
Answer: -t
-t lists.
Why does tar remove the leading / from names?
Answer: So extracting never overwrites the real system paths by accident
Files are restored relative to the current or -C directory.
Which usually gives the smallest file?
Answer: xz
xz compresses best but is slowest.
What does gzip file.log do to the original?
Answer: Replaces it with file.log.gz
Use -k to keep the original.
Which reads a .gz log without unpacking it to disk?
Answer: zcat
zcat (and xzcat, bzcat) print the decompressed text.
What does (reg >> 8) & 0xFF extract?Related
Answer: Bits 8–15 (the high byte of a 16-bit value)
Shift the field down, then mask it.
From Masks and flags
You move a blob to Archive and delete it 60 days later. What happens?Related
Answer: You pay an early deletion charge for the remaining 120 days
Archive has a 180-day minimum. Deleting at day 60 is charged as if stored for the remaining 120 days.
Why does the build name a file index-B3x9kQ1a.js instead of index.js?Related
Answer: The name changes when the content changes, so browsers can cache it for a long time safely
A new content gives a new name, so a long cache never serves an old file.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Automation basics: scheduling, archives and shell scripts” before its exam: Bash scripts: variables, quoting, arguments and exit codes, Bash scripts: conditions, loops and safe habits.
- Module 7 exam: Automation basics · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Boot, recovery and containers. 57 topics left in the course before the final exam.