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
.gzformat. - bzip2 β compresses files using the
.bz2format.
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:
| Option | Meaning |
|---|---|
-c | Create a new archive |
-x | Extract files from an archive |
-t | List contents of an archive |
-v | Verbose β show progress/details |
-f | Specify the filename of the archive |
-C | Change directory before operation (useful when extracting) |
Examples:
- Create an archive of
/etcdirectory:
tar -cvf etc-backup.tar /etc
-cβ create-vβ show files being added-fβ specify filename
- List contents of an archive:
tar -tvf etc-backup.tar
- Extract an archive:
tar -xvf etc-backup.tar
- 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:
- Compress a file:
gzip filename
- Original file
filenameis replaced byfilename.gz.
- Uncompress a file:
gunzip filename.gz
- Restores the original file.
- Keep the original file while compressing:
gzip -c filename > filename.gz
-cwrites 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:
- Compress a file:
bzip2 filename
- Creates
filename.bz2and deletes the original file.
- Uncompress:
bunzip2 filename.bz2
- 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:
- Using gzip (
.tar.gzor.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
- 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
/etcconfiguration files before upgrading a server:
tar -czvf etc-backup-$(date +%F).tar.gz /etc
- Extract a compressed log archive to
/tmpto 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.bz2commands.
8. Summary Table β Commands at a Glance
| Task | Command Example |
|---|---|
| Create tar archive | tar -cvf archive.tar /path/to/files |
| Extract tar archive | tar -xvf archive.tar |
| List tar archive | tar -tvf archive.tar |
| Create gzip compressed tar | tar -czvf archive.tar.gz /path |
| Extract gzip compressed tar | tar -xzvf archive.tar.gz |
| Create bzip2 compressed tar | tar -cjvf archive.tar.bz2 /path |
| Extract bzip2 compressed tar | tar -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
tar= archive multiple files/directories.gzip= compress individual files (fast, moderate compression).bzip2= compress individual files (slower, better compression).tar + gzip=.tar.gztar + bzip2=.tar.bz2-cβ create,-xβ extract,-tβ list-zβ gzip,-jβ bzip2-vβ verbose,-fβ filename-Cβ extract to a specific directory
