Skip to content

Intermediate · Automation basics: scheduling, archives and shell scripts · topic 36 of 57 · 30 min

Archives and compression: tar, gzip, bzip2 and xz

Explanation

tar packs many files into one archive file; gzip, bzip2 and xz compress files. tar can do both: -c creates, -x extracts, -t lists, -f names the archive, and -z, -j or -J pick gzip, bzip2 or xz. Always list (-t) before you extract, and use -C to choose where files go.

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.

Think of it like this: tar is like packing all your clothes into one suitcase so you carry one thing instead of fifty. Compression is like using a vacuum bag inside the suitcase: same clothes, less space. You can pack without the vacuum bag, but not vacuum-pack fifty loose shirts into one bag without packing them first.

The tar options you need

OptionMeaning
-ccreate an archive
-xextract
-tlist the contents
-f FILEthe archive file name (put it right after f)
-vverbose: show each file
-z / -j / -Jgzip (.tar.gz, .tgz) / bzip2 (.tar.bz2) / xz (.tar.xz)
-C DIRchange to DIR first (where to extract, or where to read from)
-pkeep permissions when extracting (default for root)
--selinux, --acls, --xattrsalso save SELinux labels, ACLs and extended attributes
bash
# 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 compression
tar strips the leading / so that extracting never overwrites the real /etc by accident.

Choosing a compressor

ToolExtensionSpeedSize
gzip.gzFastestBiggest of the three
bzip2.bz2SlowerSmaller
xz.xzSlowest to compressUsually smallest
bash
$ 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.xz

Text 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 = suitcase gzip/xz = vacuum bag

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.

bash
# 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)

Task 1 · Question

Which command creates a bzip2-compressed archive of /home?

Task 2 · Question

Which line has the bug? 1: mkdir -p /restore 2: tar -xzf -C /restore site.tar.gz

Task 3 · Code · Python

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.

Task 4 · Practice

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

Question 1

1.Which tar option lists an archive without extracting?

Question 2

2.Why does tar remove the leading / from names?

Question 3

3.Which usually gives the smallest file?

Question 4

4.What does gzip file.log do to the original?

Question 5

5.Which reads a .gz log without unpacking it to disk?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

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.

From Azure compute and storage

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.

From Building for production

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

  1. 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.
  2. Module 7 exam: Automation basics · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Boot, recovery and containers. 57 topics left in the course before the final exam.

← Previous: One-time jobs with at, and systemd timers