3.1 Archiving and Compression on the Command Line

Key Takeaways

  • tar packages many files and directories into one archive; by itself it does not shrink data
  • Mode flags: -c create, -x extract, -t list; -f must sit immediately before the archive filename
  • gzip, bzip2, and xz compress single files (.gz, .bz2, .xz); pair them with tar via -z, -j, or -J
  • .tar.gz/.tgz means a tar archive compressed with gzip; .tar.bz2 uses bzip2 instead
  • zip/unzip both archive and compress; use zip -r for directories when exchanging files across platforms
Last updated: July 2026

Archiving files on the command line (objective 3.1)

Objective 3.1 asks you to turn piles of files into portable packages and to shrink those packages when needed. On Linux Essentials, the core tools are tar, the compressors gzip, bzip2, and xz, and the cross-platform pair zip / unzip.

Archive versus compress

Keep these jobs separate — exam questions love to mix them up:

ConceptJobTypical tools
ArchiveMany files/dirs → one container, keep pathstar, also zip
CompressEncode data to use fewer bytesgzip, bzip2, xz
BothPacked tree and smallertar -czf …, zip -r …

tar (tape archive) concatenates files and directories into one stream. Alone it does not shrink data. Compression is a second step that modern tar can invoke with helper flags.


tar: create, list, extract

Almost every Essentials-level tar command uses a small flag set:

FlagMeaning
-cCreate a new archive
-xeXtract from an archive
-tlisT contents without extracting
-vVerbose — print names as they are processed
-fUse the archive file named next

Pick exactly one of -c, -x, or -t. -f must immediately precede the archive name — the next argument after -f is the archive path, not another option.

tar -cvf project.tar project/     # create
tar -tf project.tar               # list (no write)
tar -xvf project.tar              # extract here

Mnemonic order mode → verbose → file (-cvf, -tvf, -xvf) is common. GNU tar also accepts split flags (tar -c -v -f project.tar …) as long as -f still sits right before the filename.

Worked create with multiple members:

tar -cvf weekend.tar photos/ docs/readme.txt

Tar records paths relative to how you named them. Extracting later rebuilds that tree under your current working directory. Habit: cd to the destination first, then extract; list with -t before -x when the archive came from someone else.

-f trap: tar -cv backup.tar dir/ (without -f before the name) treats backup.tar as a member, not the archive file. Always glue -f to the archive name.


gzip, bzip2, and xz

These tools shrink (or expand) a single file. They do not bundle a directory tree by themselves.

gzip report.log            # → report.log.gz (original usually removed)
gunzip report.log.gz       # restore (same as gzip -d)
bzip2 dataset.csv          # → dataset.csv.bz2
bunzip2 dataset.csv.bz2
xz huge.img                # → huge.img.xz
xz -d huge.img.xz
ToolExtensionEveryday note
gzip / gunzip.gzFast, ubiquitous default
bzip2 / bunzip2.bz2Often smaller than gzip; slower
xz.xzOften smallest; slowest to compress

You do not need algorithm math for 010-160 — know names, extensions, and relative speed versus size.

Pairing compressors with tar

FilterCompressorCreateExtract / list
-zgziptar -czf a.tar.gz dir/tar -xzf / tar -tzf
-jbzip2tar -cjf a.tar.bz2 dir/tar -xjf / tar -tjf
-Jxztar -cJf a.tar.xz dir/tar -xJf / tar -tJf
tar -czf logs-2026.tar.gz /home/student/app/logs
tar -tzf logs-2026.tar.gz          # verify members first
tar -xzf logs-2026.tar.gz          # extract

Two-step form also exists: tar -cf project.tar project/ then gzip project.tarproject.tar.gz. That is why both .tar.gz and the short .tgz appear in downloads.

.tar.gz versus .tar.bz2

ExtensionMeaning
.tar.gz / .tgztar archive, then gzip
.tar.bz2tar archive, then bzip2
.tar.xztar archive, then xz
.gz / .bz2 / .xz alonecompressed single file, not a multi-file tree

If a question emphasizes common / fast backups, prefer gzip (.tar.gz). If it emphasizes smallest size and time is available, lean toward xz (or bzip2 as the middle ground). Matching the wrong filter letter — for example -z on a .tar.bz2 — is a frequent exam miss.

Worked restore: List site-backup.tar.bz2 with tar -tjf, then extract with matching -j: cd ~/restore && tar -xjf ~/Downloads/site-backup.tar.bz2.


zip and unzip

zip both archives and compresses — handy for Windows/macOS handoffs. Use zip -r site.zip site/ and unzip site.zip. Prefer tar + gzip/bzip2/xz for Linux-native backups; use zip when the audience is cross-platform. List zip contents with unzip -l before extracting.

Common exam traps

TrapWhy it fails
tar -cv backup.tar dir/ (no -f before the name)Name treated as a member, not the archive
gzip mydir/ expecting one archive of the treegzip does not package directories
-z extract on .tar.bz2Wrong filter — use -j
Swapping -c and -xCreate versus extract
unzip on .tar.gzWrong family — use tar -xzf

Exam focus checklist

  1. -f immediately before the archive filename.
  2. Match -c / -x / -t to create / extract / list.
  3. gzip alone does not archive a directory — use tar (or zip -r).
  4. Match extensions and tar letters: z→gzip, j→bzip2, J→xz.
  5. Know .tar.gz vs .tar.bz2 and when zip is the cross-platform choice.

Master the archive/compress distinction and those flag patterns, and objective 3.1 becomes reliable points on the 010-160 exam.

Test Your Knowledge

Which command creates a new gzip-compressed tar archive named backup.tar.gz from the directory reports/?

A
B
C
D
Test Your Knowledge

A teammate runs gzip on the directory project/ expecting one compressed archive of the whole tree. What is the best assessment?

A
B
C
D
Test Your Knowledge

Which statement correctly contrasts .tar.gz and .tar.bz2?

A
B
C
D
Test Your Knowledge

Which command lists the members of logs.tar.gz without writing files to disk?

A
B
C
D