Study guide
5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio & dd (103.3)
LPIC-1 Linux Administrator — Exam 101 101-500
Track your progress as you read
Finished sections get a check mark.
1 Exam Overview & Study Strategy
1.1 LPIC-1 101-500 Exam Structure & Blueprint 1.2 Command Line & Shell Fundamentals for Exam Success 1.3 Fill-in-the-Blank Strategies & Common Traps
2 Topic 101: System Architecture
2.1 Hardware Discovery, Sysfs, Procfs & Kernel Modules (101.1) 2.2 Udev, Dbus & Device Management (101.1) 2.3 BIOS vs. UEFI & System Boot Process (101.2) 2.4 Kernel Initialization, Initramfs & System Logs (101.2) 2.5 Systemd Boot Targets & Unit Management (101.3) 2.6 SysVinit Runlevels, Telinit & System Shutdown (101.3)
3 Topic 102: Linux Installation & Boot Management
3.1 Hard Disk Layout, Partition Schemes & Swap Design (102.1) 3.2 LVM & Standard Mount Point Allocation (102.1) 3.3 GRUB 2 Configuration & Troubleshooting (102.2) 3.4 Legacy GRUB & Alternative Boot Loaders (102.2) 3.5 Managing Shared Libraries & ldconfig (102.3) 3.6 Linux as a Virtualization Guest & Cloud Instances (102.6)
4 Topic 102: Package Management Systems
4.1 Debian Low-Level Package Management with dpkg (102.4) 4.2 High-Level Debian Package Management: apt, apt-get & apt-cache (102.4) 4.3 RPM Package Management with rpm (102.5) 4.4 YUM, DNF & Zypper Package Management (102.5) 4.5 Package Repositories & Integrity Verification (102.4/102.5) 4.6 Universal Linux Packages: Snap, Flatpak & AppImage (102.4/102.5)
5 Topic 103: Command Line & Text Processing
5.1 Shell Environment, Environment Variables & Aliases (103.1) 5.2 Command History, Quoting & Expansion (103.1) 5.3 Basic Text Filters: cat, head, tail, cut, paste, nl, od (103.2) 5.4 Advanced Text Filters: sort, uniq, wc, tr, split, less & Checksums (103.2) 5.5 Stream Editing with sed (103.2) 5.6 Basic File Management: cp, mv, rm, touch, mkdir, rmdir (103.3) 5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio & dd (103.3)
6 Topic 103: Streams, Processes, Regex & Vi
6.1 Standard Streams, Redirection & File Descriptors (103.4) 6.2 Pipelines, tee & xargs (103.4) 6.3 Process Monitoring: ps, top, free, watch, pgrep, pstree & uptime (103.5) 6.4 Signals, kill, Job Control, nohup, screen & tmux (103.5) 6.5 Process Priorities: nice & renice (103.6) 6.6 Regular Expressions & grep, egrep, fgrep (103.7) 6.7 vi / vim Modes, plus nano, Emacs & EDITOR Awareness (103.8)
7 Topic 104: Devices, Partitions & Filesystems
7.1 Block Devices, fdisk & gdisk (104.1) 7.2 GNU Parted & Partition Table Management (104.1) 7.3 Creating Filesystems: mkfs, ext4, XFS, VFAT/exFAT & Btrfs (104.1) 7.4 Filesystem Integrity: fsck, e2fsck, xfs_repair, xfs_fsr & xfs_db (104.2) 7.5 Monitoring Disk Usage: df, du & tune2fs (104.2) 7.6 Managing Swap Space: mkswap, swapon & swapoff (104.1/104.2)
8 Topic 104: Mounts, Permissions, Links & FHS
8.1 Mounting and Unmounting Filesystems: mount & umount (104.3) 8.2 Persistent Mounts: /etc/fstab, UUIDs & systemd mount units (104.3) 8.3 Standard File Permissions: chmod, chown, chgrp & umask (104.5) 8.4 Special Permissions: SUID, SGID & Sticky Bit (104.5) 8.5 Hard Links vs. Symbolic Links: ln & Inodes (104.6) 8.6 Filesystem Hierarchy Standard (FHS) & File Location (104.7)
5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio & dd (103.3)
Key Takeaways Explain This Topic AI
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 Flag Long Option Function & Description
- c --create Creates a new archive file
- x --extract , --get Extracts files from an existing archive
- t --list Lists table of contents of an archive without extracting
- r --append Appends files to the end of an uncompressed archive
- u --update Appends files that are newer than their copy in an uncompressed archive
- d --diff , --compare Finds differences between archive members and active filesystem files
- -delete N/A Deletes 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
- Flag Compression Utility Standard Extension Compression Type
- z gzip .tar.gz , .tgz Fast compression, moderate ratio (DEFLATE)
- j bzip2 .tar.bz2 , .tbz2 Slower compression, higher ratio (Burrows-Wheeler)
- J xz .tar.xz , .txz Slowest compression, highest ratio (LZMA2)
-Z compress .tar.Z Legacy 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 / Metric gzip bzip2 xz
- Algorithm DEFLATE (LZ77 + Huffman) Burrows-Wheeler + Huffman LZMA / LZMA2
- File Extension .gz .bz2 .xz
- tar Option Flag -z -j -J
- Compression Speed Fastest Moderate / Slow Slowest (High CPU)
- Compression Ratio Moderate High Highest
- Decompress Command gunzip (or gzip -d ) bunzip2 (or bzip2 -d ) unxz (or xz -d )
- Stdout Cat Tool zcat bzcat xzcat
- Text Pagers zless , zmore bzless , bzmore xzless , xzmore
- Regex Search zgrep , zegrep bzgrep xzgrep
- Diff Tool zdiff , zcmp bzdiff xzdiff
- 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 Flag Long Option Operation & Data Flow
- o --create Copy-Out Mode: Reads list of file paths from stdin and creates an archive on stdout
- i --extract Copy-In Mode: Reads archive from stdin and extracts files to disk
-p --pass-through Pass-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.
- Operand Meaning
- 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=progress Print 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 ( 1M – 64M 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.