Archive, compress, unpack, and uncompress files usingΒ tar,Β gzip, andΒ bzip2

1. Understand and Use Essential Tools

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


1. Introduction: Why We Archive and Compress

In Linux, you often need to:

  • Combine multiple files into a single file for easier management. This is called archiving.
  • Reduce file size to save disk space or make transferring files faster. This is called compression.

The main tools for these tasks are:

  • tar – used to create or extract archives (combines files into one).
  • gzip – compresses files using the .gz format.
  • bzip2 – compresses files using the .bz2 format.

Often, you combine tar + gzip or tar + bzip2 to archive and compress at the same time.


2. The tar Command – Archiving Tool

tar stands for tape archive. It’s used to pack multiple files/directories into one archive file.

Basic Syntax:

tar [options] archive_name files_or_directories

Key Options for RHCSA:

OptionMeaning
-cCreate a new archive
-xExtract files from an archive
-tList contents of an archive
-vVerbose – show progress/details
-fSpecify the filename of the archive
-CChange directory before operation (useful when extracting)

Examples:

  1. Create an archive of /etc directory:
tar -cvf etc-backup.tar /etc
  • -c β†’ create
  • -v β†’ show files being added
  • -f β†’ specify filename
  1. List contents of an archive:
tar -tvf etc-backup.tar
  1. Extract an archive:
tar -xvf etc-backup.tar
  1. Extract to a specific directory:
tar -xvf etc-backup.tar -C /tmp

Tip: On the exam, they may ask you to extract files somewhere specific, so remember -C.


3. Compression with gzip and gunzip

gzip compresses files, creating .gz files. Unlike tar, it compresses individual files, not directories.

Basic Usage:

  1. Compress a file:
gzip filename
  • Original file filename is replaced by filename.gz.
  1. Uncompress a file:
gunzip filename.gz
  • Restores the original file.
  1. Keep the original file while compressing:
gzip -c filename > filename.gz
  • -c writes output to a new file and keeps the original.

Important: On the exam, you may be asked to compress multiple files with gzip using tar.


4. Compression with bzip2 and bunzip2

bzip2 is another compression tool. It generally compresses better than gzip (smaller file size), but is slightly slower.

Basic Usage:

  1. Compress a file:
bzip2 filename
  • Creates filename.bz2 and deletes the original file.
  1. Uncompress:
bunzip2 filename.bz2
  1. Keep the original file:
bzip2 -k filename

5. Combining tar with gzip or bzip2

Most often in IT environments, we archive directories AND compress at the same time:

  1. Using gzip (.tar.gz or .tgz):
tar -czvf backup.tar.gz /var/log
  • -c β†’ create
  • -z β†’ compress with gzip
  • -v β†’ verbose
  • -f β†’ file name

Extract .tar.gz:

tar -xzvf backup.tar.gz
  • -x β†’ extract
  • -z β†’ gzip
  • -v β†’ verbose
  • -f β†’ filename
  1. Using bzip2 (.tar.bz2):
tar -cjvf backup.tar.bz2 /var/log
  • -j β†’ compress with bzip2

Extract .tar.bz2:

tar -xjvf backup.tar.bz2

Exam Tip: -z β†’ gzip, -j β†’ bzip2. They test if you know the difference.


6. Listing Contents Without Extracting

Sometimes you need to see what’s inside an archive before extracting:

  • .tar β†’ tar -tvf archive.tar
  • .tar.gz β†’ tar -tzvf archive.tar.gz
  • .tar.bz2 β†’ tar -tjvf archive.tar.bz2

7. Practical IT Use Examples (Exam Style)

  • Back up /etc configuration files before upgrading a server:
tar -czvf etc-backup-$(date +%F).tar.gz /etc
  • Extract a compressed log archive to /tmp to check logs:
tar -xzvf logs.tar.gz -C /tmp
  • Compress a single database dump to save space:
gzip /backup/db.sql

Tip for RHCSA: They may give you a directory and ask you to create a compressed archive in a certain format. You must know: .tar, .tar.gz, .tar.bz2 commands.


8. Summary Table – Commands at a Glance

TaskCommand Example
Create tar archivetar -cvf archive.tar /path/to/files
Extract tar archivetar -xvf archive.tar
List tar archivetar -tvf archive.tar
Create gzip compressed tartar -czvf archive.tar.gz /path
Extract gzip compressed tartar -xzvf archive.tar.gz
Create bzip2 compressed tartar -cjvf archive.tar.bz2 /path
Extract bzip2 compressed tartar -xjvf archive.tar.bz2
Compress single file (gzip)gzip file
Uncompress single file (gzip)gunzip file.gz
Compress single file (bzip2)bzip2 file
Uncompress single file (bzip2)bunzip2 file.bz2

βœ… Exam Key Points to Remember

  1. tar = archive multiple files/directories.
  2. gzip = compress individual files (fast, moderate compression).
  3. bzip2 = compress individual files (slower, better compression).
  4. tar + gzip = .tar.gz
  5. tar + bzip2 = .tar.bz2
  6. -c β†’ create, -x β†’ extract, -t β†’ list
  7. -z β†’ gzip, -j β†’ bzip2
  8. -v β†’ verbose, -f β†’ filename
  9. -C β†’ extract to a specific directory
Buy Me a Coffee