4.3 Archive and Compress with tar, gzip, and bzip2
Key Takeaways
- tar bundles many files into one archive; common create/extract patterns use tar -cf / tar -xf, with compression added via z (gzip) or j (bzip2) flags or equivalent modern auto options.
- gzip and bzip2 compress individual files by default (replacing the original unless you use options that keep it); tar with -z or -j compresses the whole archive stream.
- Memorize create vs extract: c = create, x = extract, t = list, f = archive file name; v = verbose helps verify on the exam.
- Preserve ownership and SELinux contexts when required using tar options such as --selinux / appropriate preserve flags, and extract carefully with paths in mind.
- Test archives with tar -tf before extracting over important trees; wrong working directory or absolute paths in archives are classic recovery mistakes.
4.3 Archive and Compress with tar, gzip, and bzip2
Quick Answer: Create a gzip-compressed tarball with
tar czf archive.tar.gz tree/, list withtar tzf archive.tar.gz, extract withtar xzf archive.tar.gz. Usejinstead ofzfor bzip2 (.tar.bz2). Standalonegzip/bzip2compress single files;taris for multi-file trees.
The EX200 skill to archive, compress, unpack, and uncompress files using tar, gzip, and bzip2 is the exact wording Red Hat publishes for the RHEL 10 exam. Older revisions of the objective list also named star; the current list does not. This skill appears whenever you backup a tree, ship logs, or unpack vendor content. On RHEL, these tools are always available in a normal base environment—do not depend on exotic compressors unless the task installs them.
Why Archive and Compress Are Separate Ideas
| Concept | Meaning | Typical tool |
|---|---|---|
| Archive | Combine many files/directories into one file, preserving structure | tar |
| Compress | Shrink a byte stream to save space | gzip, bzip2, xz |
| Compressed archive | Archive then compress (or compress while archiving) | tar + z/j |
A .tar file is an archive that may still be large. A .gz file is usually a single compressed stream. A .tar.gz (or .tgz) is a tarball compressed with gzip. A .tar.bz2 uses bzip2.
tar Fundamentals
tar originally meant “tape archive.” Today it writes archive files just as often as tapes.
Mode letters you must know
| Letter | Mode |
|---|---|
c | Create archive |
x | Extract archive |
t | List contents (table of contents) |
f | Use archive file name that follows (almost always required for file archives) |
v | Verbose — list members as they are processed |
z | Filter through gzip |
j | Filter through bzip2 |
J | Filter through xz (awareness; gzip/bzip2 remain exam staples) |
p | Preserve permissions (extract; often default for root) |
Traditional style concatenates letters after a dash (or even without a dash on some examples):
tar -cf backup.tar project/
tar -tf backup.tar
tar -xf backup.tar
With compression:
tar -czf backup.tar.gz project/
tar -tzf backup.tar.gz
tar -xzf backup.tar.gz
tar -cjf backup.tar.bz2 project/
tar -tjf backup.tar.bz2
tar -xjf backup.tar.bz2
Memory hook: create / xtract / test-list + file + z gzip or j bzip2. The f archive name must immediately associate with the file operand—put the archive filename right after the options that include f (common form: tar czf name.tar.gz ...).
Create examples
cd /home/student
tar czf /root/student-project.tgz project
tar czf app-backup-$(date +%F).tar.gz /srv/app
tar cjf /backup/var-log.tar.bz2 -C / var/log
-C / changes directory before adding members so the archive stores var/log/... rather than an absolute /var/log/... layout—often cleaner for portable restores.
Extract examples
mkdir -p /tmp/restore && cd /tmp/restore
tar xzf /root/student-project.tgz
tar xjf /backup/var-log.tar.bz2
Extract specific members:
tar xzf backup.tar.gz path/inside/archive/file.conf
List before you extract into a sensitive location:
tar tzf backup.tar.gz | head
tar tzf backup.tar.gz | grep 'etc/hosts'
Exam trap: Extracting as the wrong user, in the wrong directory, or overwriting live configs without a listing pass. Always pwd and prefer extracting into an empty staging directory first when unsure.
Absolute paths and tar
Older habits sometimes archived absolute paths. GNU tar often strips leading / on extract by default (safer), but you should still inspect with tar tf so you know whether members look like etc/hosts or /etc/hosts. Do not extract untrusted archives as root over / without understanding contents.
gzip and bzip2 as Standalone Tools
gzip
gzip large.log # creates large.log.gz and removes large.log by default
gzip -k large.log # keep original if supported (gzip --keep)
gunzip large.log.gz # restore
zcat large.log.gz # view without writing decompressed file
gzip -d large.log.gz # same idea as gunzip
Characteristics:
- Very common on RHEL for logs and tarballs
- Fast compression and decompression
- Typically smaller gains than bzip2 on some text, but quicker
bzip2
bzip2 bigdump.sql # creates bigdump.sql.bz2, removes original by default
bzip2 -k bigdump.sql # keep input when supported
bunzip2 bigdump.sql.bz2
bzcat bigdump.sql.bz2 | less
Characteristics:
- Often better compression ratio than gzip on highly redundant data
- Slower compress/decompress—noticeable on large trees
- Pairs with tar via
j→.tar.bz2
Single-file vs tar-of-many
# Wrong mental model for a directory:
gzip mydir # does not archive a directory tree the way tar does
# Right approach for a directory:
tar czf mydir.tar.gz mydir
gzip/bzip2 operate on files (streams). Directories need tar (or another archiver) first.
Combined Workflows You Will Use on the Exam
Backup a project tree with gzip
tar czf /backup/website-$(date +%F).tar.gz -C /var/www html
ls -lh /backup/website-*.tar.gz
tar tzf /backup/website-*.tar.gz | head
Backup with bzip2 when size matters more than speed
tar cjf /backup/website.tar.bz2 -C /var/www html
Unpack vendor content
cd /usr/local/src
tar xzf /tmp/app-1.2.3.tar.gz
cd app-1.2.3
Compress a single large log without tar
gzip -k /var/log/myapp/old.log
ls -lh /var/log/myapp/old.log*
Pipe tar across a stream (conceptual)
tar czf - project | ssh host 'cat > /backup/project.tgz'
# or extract from stdin on the far side—patterns appear in multi-host labs
Preserving Attributes and Special Files
For ordinary user trees, default tar create/extract is often enough. For system backups you may care about:
- Permissions and ownership (extract as root to restore original owners)
- SELinux contexts (GNU tar options such as
--selinuxwhen creating/extracting in SELinux environments) - Sparse files, device nodes, and ACLs on advanced backups
RHCSA-level tasks usually focus on getting the correct tree back with expected modes. If you archive as root and extract as an unprivileged user into your home directory, ownership becomes yours—verify with ls -l after extract when ownership matters.
cp -a (previous section) is still better for live local clones. Use tar when you need a single portable file, compression, or transfer packaging.
star: legacy objective wording only
Older EX200 objective lists (RHEL 7/8 era) named star (Schily tar) alongside tar. The current RHEL 10 objective names only tar, gzip, and bzip2, and star is not part of a standard RHEL 10 install. Treat any study material that still drills star as out of date for this exam. Master GNU tar + gzip + bzip2; do not spend scarce minutes learning a second archiver that the objective no longer lists.
Verification Checklist After Create/Extract
- Archive exists and size is plausible:
ls -lh backup.tar.gz - Contents list looks right:
tar tzf backup.tar.gz | head/grep - Extract into staging: empty directory, then compare with
diff -rorls -lR - Spot-check a known file’s content and mode
- Clean up temporary copies so you do not leave wrong trees in graded paths
mkdir -p /tmp/tarcheck && cd /tmp/tarcheck
tar xzf /backup/website.tar.gz
find . -type f | head
Common Pitfalls
- Swapping create and extract —
tar xzfon a non-archive path errors;tar czfwith wrong argument order can create an archive named after a file you meant to include. - Forgetting
f— tar may try to use a default tape device. - Wrong compression letter — using
zon a.bz2archive (or the reverse) produces errors; list with the matching filter or use moderntarauto-detect forms where available (tar xf file.tar.gzoften works on GNU tar). - gzip on a directory — does not replace
tar. - Extracting in
/or/etcaccidentally — always checkpwdand archive member prefixes. - Assuming compression keeps originals — classic
gzip fileremovesfileunless keep is requested. - Broken workflows with symlinks — decide whether you need to store symlinks as links (
tardoes) or follow them; know that extracting may recreate links that break if targets are outside the archive.
Quick Reference Card
| Goal | Command pattern |
|---|---|
| Create gzip tarball | tar czf arch.tar.gz dir/ |
| Create bzip2 tarball | tar cjf arch.tar.bz2 dir/ |
| List gzip tarball | tar tzf arch.tar.gz |
| Extract gzip tarball | tar xzf arch.tar.gz |
| Extract bzip2 tarball | tar xjf arch.tar.bz2 |
| Compress one file gzip | gzip file → file.gz |
| Compress one file bzip2 | bzip2 file → file.bz2 |
| Decompress | gunzip file.gz / bunzip2 file.bz2 |
Practice Drill
mkdir -p /tmp/tarlab/src/sub
echo one > /tmp/tarlab/src/a.txt
echo two > /tmp/tarlab/src/sub/b.txt
ln -s a.txt /tmp/tarlab/src/link-to-a
cd /tmp/tarlab
tar czf src-gzip.tar.gz src
tar cjf src-bzip.tar.bz2 src
tar tzf src-gzip.tar.gz
mkdir out-gz out-bz
tar xzf src-gzip.tar.gz -C out-gz
tar xjf src-bzip.tar.bz2 -C out-bz
ls -lR out-gz out-bz
gzip -k out-gz/src/a.txt
ls -l out-gz/src/a.txt*
Confirm the symlink survived packaging, both compression flavors unpack to matching trees, and standalone gzip produced a.txt.gz as expected.
Section Checkpoint
You should be able to create and extract .tar.gz and .tar.bz2 archives, list members before unpacking, compress and decompress individual files with gzip and bzip2, choose tar when directories are involved, and avoid path and overwrite mistakes during restore. Combined with solid cp/mv/ln skills, you can stage, package, and reassemble any file tree the exam throws at you.
Which command creates a gzip-compressed archive named /root/app.tgz from the directory /srv/app?
You need to inspect the member list of logs.tar.bz2 without writing files to disk. Which approach is correct?
What happens by default when you run gzip report.csv on RHEL?
Which command extracts the contents of backup.tar.gz into the current directory?