5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio & dd (103.3)

Key Takeaways

  • tar bundles files into archives; exactly one main operation mode (-c create, -x extract, -t list) must be selected.
  • The -f flag specifies the archive filename and MUST immediately precede that filename; tar's integrated compression flags are -z (gzip, .tar.gz), -j (bzip2, .tar.bz2), and -J (xz, .tar.xz).
  • Compression algorithms scale across speed vs ratio: gzip is fastest with moderate ratio, bzip2 offers higher ratio, and xz (LZMA2) yields highest ratio.
  • cpio operates in copy-out (-o), copy-in (-i), or pass-through (-p) modes, processing file lists from stdin (find | cpio) and formatting with -H newc.
  • dd copies raw blocks using operand=value syntax (if=, of=, bs=, count=, conv=), so it can image an MBR (bs=512 count=1), clone a disk, or create a swap file; it never prompts before overwriting of=.
Last updated: August 2026

5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio (103.3)

Quick Summary: Linux separates archiving (combining multiple files, directories, and metadata into a single container stream) from compression (reducing raw byte size). The tar (Tape Archive) and cpio utilities create archive bundles, while gzip, bzip2, and xz perform data compression. Understanding integrated flags (-z, -j, -J), compression ratios, and pipeline mechanics is essential for Topic 103.3.


1. Archiving vs. Compression Concepts

  • Archiving: Aggregates hundreds or thousands of individual files, directory hierarchies, permissions, ownerships, and timestamps into a single uncompressed stream (e.g., .tar or .cpio).
  • Compression: Applies mathematical entropy encoding algorithms (DEFLATE, Burrows-Wheeler, LZMA2) to reduce file size. Standard Linux compression utilities (gzip, bzip2, xz) compress only single files at a time; they do not package directory structures.
  • Combined Workflow (.tar.gz, .tar.bz2, .tar.xz): tar packages the directory structure into a single archive file, which is then compressed using a compression tool.

2. tar (Tape Archive) Deep Dive

tar Command Anatomy & Option Rules:
tar  [Operation Mode: MUST choose ONE]  [Modifiers & Compression]  -f [Archive File]  [Targets]
tar             -c                            -z -v             -f backup.tar.gz    /etc /var/log

Primary Operation Modes (Choose Exactly ONE)

Mode FlagLong OptionFunction & Description
-c--createCreates a new archive file
-x--extract, --getExtracts files from an existing archive
-t--listLists table of contents of an archive without extracting
-r--appendAppends files to the end of an uncompressed archive
-u--updateAppends files that are newer than their copy in an uncompressed archive
-d--diff, --compareFinds differences between archive members and active filesystem files
--deleteN/ADeletes files from an uncompressed archive

Operation Modifiers & Flags

  • -f <file> (--file): Specifies the archive file name or device. CRITICAL SYNTAX RULE: The -f flag must immediately precede the archive filename. In tar -czvf archive.tar.gz /data, -f is last in the cluster so archive.tar.gz is parsed as the file. If written tar -czfv archive.tar.gz /data, v would be treated as the archive filename!
  • -v (--verbose): Lists file names as they are archived or extracted.
  • -p (--preserve-permissions): Preserves original file permissions (default for superuser).
  • -C <dir> (--directory): Changes directory to <dir> before performing operations (e.g., tar -xzf app.tar.gz -C /opt/).
  • --exclude=<pattern>: Excludes files matching globbing pattern from the archive.
  • -W (--verify): Attempts to verify the archive after writing.

Built-in Compression Modifiers

FlagCompression UtilityStandard ExtensionCompression Type
-zgzip.tar.gz, .tgzFast compression, moderate ratio (DEFLATE)
-jbzip2.tar.bz2, .tbz2Slower compression, higher ratio (Burrows-Wheeler)
-Jxz.tar.xz, .txzSlowest compression, highest ratio (LZMA2)
-Zcompress.tar.ZLegacy Unix compression algorithm
# Creating a compressed tarball with gzip:
sudo tar -czvf /backup/etc_backup.tar.gz /etc

# Listing contents of a bzip2 compressed archive without extracting:
tar -tjvf /backup/logs.tar.bz2

# Extracting an xz compressed archive into a specific target directory:
sudo tar -xJvf release.tar.xz -C /opt/production/

3. Compression Utilities Comparison: gzip, bzip2, xz

Feature / Metricgzipbzip2xz
AlgorithmDEFLATE (LZ77 + Huffman)Burrows-Wheeler + HuffmanLZMA / LZMA2
File Extension.gz.bz2.xz
tar Option Flag-z-j-J
Compression SpeedFastestModerate / SlowSlowest (High CPU)
Compression RatioModerateHighHighest
Decompress Commandgunzip (or gzip -d)bunzip2 (or bzip2 -d)unxz (or xz -d)
Stdout Cat Toolzcatbzcatxzcat
Text Pagerszless, zmorebzless, bzmorexzless, xzmore
Regex Searchzgrep, zegrepbzgrepxzgrep
Diff Toolzdiff, zcmpbzdiffxzdiff

Common Compression Operational Flags

  • Replaces original by default: When running gzip file.txt, the original file.txt is deleted and replaced by file.txt.gz.
  • -d (--decompress): Decompresses the file (e.g., gzip -d file.gz is identical to gunzip file.gz).
  • -c (--stdout): Writes output to standard output, preserving the original file (gzip -c file.txt > file.txt.gz).
  • -k (--keep): Keeps (does not delete) input files during compression or decompression (supported in gzip, bzip2, xz).
  • -1 to -9: Compression levels (-1 or --fast is fastest with lowest ratio; -9 or --best is slowest with maximum ratio; default is usually -6).
  • -l (--list): Lists uncompressed size, compressed size, and ratio (available in gzip and xz).
# Inspecting compressed log files directly without decompressing to disk:
zcat /var/log/syslog.2.gz | grep "CRON"
zgrep "Failed password" /var/log/auth.log.*.gz
xzless /usr/share/doc/kernel/changelog.xz

💡 LPIC-1 Exam Fill-in-the-Blank Alert: To decompress a .bz2 file from the command line while keeping the original file, use bunzip2 -k or bzip2 -dk. To view the contents of a gzip-compressed text file directly to stdout, use zcat.

Loading diagram...
cpio Pipeline Archiving and Extraction Architecture

4. cpio: Copy In/Out Archiving

The cpio (Copy In/Out) tool processes archives by reading file lists from standard input (stdin) or writing archive streams to standard output (stdout).

Primary Operation Modes (Choose Exactly ONE)

Mode FlagLong OptionOperation & Data Flow
-o--createCopy-Out Mode: Reads list of file paths from stdin and creates an archive on stdout
-i--extractCopy-In Mode: Reads archive from stdin and extracts files to disk
-p--pass-throughPass-Through Mode: Copies files directly from one directory tree to another without creating an archive file

Essential cpio Modifiers

  • -v (--verbose): Lists file names processed.
  • -t (--list): Prints table of contents (used with -i: cpio -it < archive.cpio).
  • -d (--make-directories): Creates leading directories where needed during extraction.
  • -u (--unconditional): Replaces existing files without prompting.
  • -m (--preserve-modification-time): Preserves original file modification timestamps.
  • --null / -0: Reads null-delimited (\0) filenames from stdin (pairs with find -print0).
  • -H <format> (--format): Specifies archive format. Standard formats:
    • newc: SVR4 portable format with CRC/inodes (mandatory standard for Linux initramfs images!).
    • crc: SVR4 portable format with computed CRC checksum.
    • tar / ustar: Standard POSIX tar format.
    • odc: Old POSIX.1 portable format.
# Creating a modern portable cpio archive using find pipeline:
$ find . -depth -print0 | cpio --null -ov -H newc > /backup/tree.cpio

# Viewing table of contents of a cpio archive:
$ cpio -itv < /backup/tree.cpio

# Extracting all files and creating directories as needed:
$ cpio -idmv < /backup/tree.cpio

# Direct directory tree cloning using pass-through mode:
$ find /var/data -depth -print0 | cpio --null -pdmv /mnt/storage/

5. dd: Raw Block-Level Copying

Objective 103.3 names three archiving utilities: tar, cpio and dd. dd is the odd one out — it does not understand files, directories, permissions, or ownership. It copies blocks of bytes from an input to an output, which is precisely why it can duplicate a boot sector, clone a whole disk, or manufacture a swap file when no filesystem-aware tool can.

Operand Syntax (Not Options)

dd uses operand=value syntax rather than dashes — writing dd -if=/dev/sda is a syntax error, and the exam tests this.

OperandMeaning
if=Input file (defaults to stdin)
of=Output file (defaults to stdout)
bs=Block size for both reading and writing
ibs= / obs=Separate input / output block sizes
count=Copy only this many blocks
skip=Skip N input blocks before copying
seek=Skip N output blocks before writing
conv=Conversions: sync, noerror, notrunc, fdatasync
status=progressPrint running transfer statistics

Suffixes multiply the number: bs=1K (1024), bs=1M, bs=1G.

Canonical Exam Scenarios

# 1. Create a 2 GiB zero-filled file (the standard swap-file first step)
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

# 2. Back up the 512-byte MBR (446 bytes of boot code + 64-byte partition table + 2-byte signature)
$ sudo dd if=/dev/sda of=/root/mbr.bak bs=512 count=1

# 3. Back up ONLY the boot code, leaving the partition table untouched
$ sudo dd if=/dev/sda of=/root/bootcode.bak bs=446 count=1

# 4. Clone one disk onto another of equal or larger size
$ sudo dd if=/dev/sda of=/dev/sdb bs=64M conv=noerror,sync status=progress

# 5. Write an installer image to a USB stick (target the DISK, not a partition)
$ sudo dd if=debian-12.5.0-amd64-netinst.iso of=/dev/sdb bs=4M conv=fdatasync status=progress

# 6. Securely overwrite a partition with random data before disposal
$ sudo dd if=/dev/urandom of=/dev/sdc1 bs=1M status=progress

conv= Values Worth Memorising

  • conv=noerror — continue after read errors instead of aborting. Essential when imaging a failing disk.
  • conv=sync — pad short input blocks with NUL bytes so the output stays block-aligned. Almost always paired with noerror, because skipping a bad block without padding would shift every subsequent byte.
  • conv=notrunc — do not truncate the output file. Required when patching bytes into the middle of an existing image.
  • conv=fdatasync — flush the write cache before dd exits, so the command does not report success while data is still queued in RAM.

Monitoring a Long Transfer

# Modern GNU coreutils: built-in progress
$ sudo dd if=/dev/sda of=/backup/sda.img bs=64M status=progress

# Portable alternative: SIGUSR1 makes dd print statistics without stopping
$ sudo kill -USR1 $(pgrep -x dd)

Exam Trap — of= Overwrites Without Warning: dd has earned the nickname "disk destroyer." There is no confirmation prompt. Writing of=/dev/sda instead of of=/dev/sdb destroys the partition table and filesystem of the running system instantly. Always confirm the target with lsblk immediately before pressing Enter.

Exam Rule: Larger bs= values generally raise throughput up to a point (1M64M is the practical sweet spot), because each block is one read syscall and one write syscall. The default bs=512 is correct only when you genuinely need 512-byte granularity, such as MBR work.

Test Your Knowledge

Which tar command line correctly creates a bzip2-compressed archive named etc_backup.tar.bz2 containing the /etc directory, with verbose output?

A
B
C
D
Test Your Knowledge

A Linux administrator needs to search for occurrences of the word 'kernel' inside a compressed log file named syslog.1.gz without decompressing the file to disk. Which utility should be used?

A
B
C
D
Test Your Knowledge

Which cpio command pipeline correctly creates a new archive in the modern 'newc' format containing all files found in the current directory tree?

A
B
C
D