Archiving and compression: tar, gzip, bzip2, zip; package management commands

Linux Essentials Preparation · lesson 10 of 23 · 25 min

Archiving with tar

tar (Tape Archive) bundles multiple files into a single archive file (.tar). It does NOT compress by default — compression is added with flags.

Key tar flags: c = create, x = extract, t = list contents, v = verbose, f = specify filename, z = gzip compression, j = bzip2 compression, J = xz compression.

Create a gzip-compressed archive: tar -czvf archive.tar.gz /path/to/dir

Extract a gzip archive: tar -xzvf archive.tar.gz

Extract to a specific directory: tar -xzvf archive.tar.gz -C /destination/

List contents without extracting: tar -tzvf archive.tar.gz

Create a bzip2 archive (better compression, slower): tar -cjvf archive.tar.bz2 /path/

Mnemonic for tar flags

Remember: create, extract, table of contents, verbose, file. Always put f last (it takes the filename argument). Example: tar -czvf = Create Zip Verbose File.

Compression Tools

gzip — compresses a single file, replaces it with .gz version. gzip file → file.gz. Decompress: gunzip file.gz or gzip -d file.gz. Fast, moderate compression.

bzip2 — better compression than gzip, slower. bzip2 file → file.bz2. Decompress: bunzip2 file.bz2.

xz — best compression ratio, slowest. xz file → file.xz. Decompress: unxz file.xz.

zip / unzip — cross-platform archive + compression. zip archive.zip file1 file2, zip -r archive.zip dir/. Decompress: unzip archive.zip. Preferred when sharing with Windows users.

Compression comparison: xz > bzip2 > gzip (compression ratio). gzip > bzip2 > xz (speed).

Package Management

Package managers install, update, and remove software. The correct tool depends on the Linux distribution family.

Debian/Ubuntu family — APT:

apt update — refresh package index from repositories.

apt upgrade — upgrade all installed packages.

apt install package — install a package.

apt remove package — remove a package (keep config files).

apt purge package — remove package and config files.

apt search keyword — search for packages.

dpkg -l — list installed packages. dpkg -i package.deb — install a .deb file.

RHEL/CentOS/Fedora family — DNF/YUM:

dnf install package, dnf update, dnf remove package, dnf search keyword.

rpm -qa — list all installed RPM packages. rpm -ivh package.rpm — install an RPM file.

Arch Linux — pacman: pacman -S package (install), pacman -Syu (update all), pacman -R package (remove).

Key Terms

Key Terms

Knowledge Check — 12 Questions

Linux — Archiving & Package Management

Which tar command creates a gzip-compressed archive of /home/user?

  • tar -xzvf backup.tar.gz /home/user
  • tar -czvf backup.tar.gz /home/user
  • tar -tzvf backup.tar.gz /home/user
  • tar -cjvf backup.tar.gz /home/user

Answer: tar -czvf backup.tar.gz /home/user
-c = create, -z = gzip, -v = verbose, -f = filename. This creates a gzip-compressed archive.

Which tar flag extracts an archive?

  • -c
  • -x
  • -t
  • -v

Answer: -x
-x extracts the archive. -c creates, -t lists contents, -v is verbose.

To list the contents of a tar archive without extracting, use:

  • tar -xvf archive.tar
  • tar -tvf archive.tar
  • tar -cvf archive.tar
  • tar -lvf archive.tar
  • Answer: tar -tvf archive.tar

-t (table of contents) lists archive contents without extracting. Add -v for verbose output.

Which compression tool provides the best compression ratio?

  • gzip
  • bzip2
  • xz
  • zip

Answer: xz
xz provides the best compression ratio but is the slowest. gzip is fastest but compresses less.

Which package manager is used on Ubuntu/Debian systems?

  • dnf
  • yum
  • pacman
  • apt

Answer: apt
apt (Advanced Package Tool) is the package manager for Debian/Ubuntu. dnf/yum is for RHEL/CentOS/Fedora.

Before installing packages on Ubuntu, you should run apt _____ to refresh the package index.

Answer: update
apt update refreshes the local package index from repositories — always run before apt install or upgrade.

apt purge differs from apt remove because:

  • purge is faster
  • purge also removes configuration files
  • purge only removes dependencies
  • They are identical
  • Answer: purge also removes configuration files

apt remove removes the package but keeps config files. apt purge removes the package AND its configuration files.

Which command installs a .deb package file directly?

  • apt install package.deb
  • dpkg -i package.deb
  • rpm -ivh package.deb
  • yum install package.deb

Answer: dpkg -i package.deb
dpkg -i installs a .deb file directly. apt handles dependencies automatically; dpkg does not.

zip is preferred over gzip when:

  • Maximum compression is needed
  • Sharing files with Windows users
  • Archiving system backups
  • Speed is critical
  • Answer: Sharing files with Windows users

zip is cross-platform and natively supported on Windows — preferred when sharing files across operating systems.

Which command lists all installed RPM packages?

  • rpm -qa
  • rpm -l
  • dnf list
  • yum installed

Answer: rpm -qa
rpm -qa (query all) lists all installed RPM packages. Pipe to grep to search: rpm -qa | grep httpd.

To extract a tar.bz2 archive, which flag combination is correct?

  • -xzvf
  • -xjvf
  • -xcvf
  • -xJvf

Answer: -xjvf
-x = extract, -j = bzip2, -v = verbose, -f = filename. Use -z for gzip (.gz), -j for bzip2 (.bz2), -J for xz (.xz).

Which package manager is used on Arch Linux?

  • apt
  • dnf
  • pacman
  • zypper

Answer: pacman
pacman is the package manager for Arch Linux. apt = Debian/Ubuntu, dnf = RHEL/Fedora, zypper = openSUSE.


Previous lesson: File operations: cp, mv, rm, mkdir, find, grep, sort, wc, head, tail, cat, less
Back to Linux Essentials Preparation
Next lesson: Shell scripting basics: variables, conditionals, loops, functions, shebang