File Compression and Archiving with tar, gzip, and bzip2 in RHEL
Compressing and archiving files is one of the most common tasks you will perform as a Linux sysadmin — whether you are backing up configuration files, transferring large directories to remote systems, or preparing data for long-term storage. RHEL provides native tools to handle both tasks efficiently.
In this guide you will learn how to use gzip, bzip2, and tar with real lab examples from a RHEL system.
Why Compress and Archive?
Compressing one or more large files or an entire directory hierarchy lets you:
- Conserve disk space on the server
- Transfer files faster over the network
- Bundle hundreds of files into a single portable archive
- Preserve file attributes like permissions, ownership, and SELinux contexts
The typical workflow is to archive first with tar, then compress — or do both in a single command.
gzip and gunzip
The gzip/gunzip pair has been available in Linux for over two decades. It is the most widely used compression tool on RHEL systems.
How gzip works
- Compresses each specified file and adds the
.gzextension - The original file is replaced by the compressed version
- Use
-rto compress an entire directory tree - Use
-lto display compression information about a gzipped file
Lab — Compressing a file with gzip
First, copy the /etc/fstab file to the root home directory and verify:
[root@rhel ~]# cp /etc/fstab /root/fstab
[root@rhel ~]# ls -lh /root/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 fstabNow compress with gzip and confirm:
[root@rhel ~]# gzip fstab
[root@rhel ~]# ls -lh /root/fstab.gz
-rw-r--r--. 1 root root 401 Mar 24 09:00 fstab.gzThe original file is gone and replaced by fstab.gz. View compression info:
[root@rhel ~]# gzip -l fstab.gz
compressed uncompressed ratio uncompressed_name
401 617 35.0% fstabLab — Decompressing with gunzip
[root@rhel ~]# gunzip fstab.gz
[root@rhel ~]# ls -lh /root/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 fstabThe file is restored to its original size with the same timestamp and attributes.
bzip2 and bunzip2
The bzip2/bunzip2 pair works similarly to gzip but with a better compression ratio — it produces smaller files. The trade-off is that it is slower, especially on large files.
Lab — Compressing with bzip2
[root@rhel ~]# bzip2 fstab
[root@rhel ~]# ls -lh /root/fstab.bz2
-rw-r--r--. 1 root root 385 Mar 24 09:00 fstab.bz2Notice the .bz2 extension. The file is slightly smaller than the gzip version.
Lab — Decompressing with bunzip2
[root@rhel ~]# bunzip2 fstab.bz2
[root@rhel ~]# ls -lh /root/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 fstabFile is fully restored with original attributes intact.
gzip vs bzip2 — Which One to Use?
| Feature | gzip | bzip2 |
|---------|------|-------|
| Speed | Faster | Slower |
| Compression ratio | Good | Better (smaller files) |
| Extension | .gz | .bz2 |
| Best for | Quick compressions, small files | Large files, maximum compression |
| Decompress command | gunzip or gzip -d | bunzip2 or bzip2 -d |
For most daily tasks, gzip is the go-to choice. When file size matters more than speed — such as archiving logs for long-term storage — use bzip2.
tar — The Archive Command
The tar (tape archive) command is used to create, list, and extract archives — called tarballs or tarfiles. By itself, tar does not compress. But it can call gzip or bzip2 directly to produce a compressed archive in one step.
Key tar options
| Option | Description |
|--------|-------------|
| -c | Creates a tarball |
| -f | Specifies the tarball filename |
| -t | Lists contents of a tarball |
| -x | Extracts files from a tarball |
| -v | Verbose mode — shows files being processed |
| -p | Preserves file permissions |
| -r | Appends files to an existing uncompressed tarball |
| -u | Appends only if files are newer than existing archive |
| -z | Compresses with gzip |
| -j | Compresses with bzip2 |
> Note: The -r and -u options do not work on compressed tarballs.
Lab — Creating and Working with tar Archives
Create a tarball of the /home directory
[root@rhel ~]# tar -cvf /tmp/home.tar /home
tar: Removing leading '/' from member names
home/
home/abernal/
home/abernal/.bash_profile
home/abernal/.bashrc
home/abernal/.bash_logoutThe first line — tar: Removing leading '/' from member names — is important. By default, tar strips the leading / from paths. This gives you the flexibility to restore files to any location without being forced to use the full original path.
List contents of a tarball
[root@rhel ~]# tar -tvf /tmp/home.tar
drwxr-xr-x root/root 0 2026-03-24 09:00 home/
drwx------ abernal/abernal 0 2026-03-24 09:00 home/abernal/
-rw-r--r-- abernal/abernal 141 2026-03-24 09:00 home/abernal/.bash_profileArchive specific files
[root@rhel ~]# tar -cvf /tmp/files.tar /etc/fstab /etc/hosts
tar: Removing leading '/' from member names
etc/fstab
etc/hostsAppend files to an existing tarball
[root@rhel ~]# tar -rvf /tmp/files.tar /etc/yum.repos.d/
etc/yum.repos.d/
etc/yum.repos.d/redhat.repoExtract a single file from a tarball
[root@rhel ~]# tar -xvf /tmp/files.tar etc/fstab
etc/fstab
[root@rhel ~]# ls -l etc/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 etc/fstabExtract all files from a tarball
[root@rhel ~]# tar -xvf /tmp/files.tar
etc/fstab
etc/hosts
etc/yum.repos.d/
etc/yum.repos.d/redhat.repoLab — Creating Compressed Archives (tar + gzip/bzip2)
This is the most common real-world usage — archive and compress in a single command.
Create a gzip-compressed tarball
[root@rhel ~]# tar -czvf /tmp/home.tar.gz /home
tar: Removing leading '/' from member names
home/
home/abernal/
home/abernal/.bash_profileCreate a bzip2-compressed tarball
[root@rhel ~]# tar -cjvf /tmp/home.tar.bz2 /home
tar: Removing leading '/' from member names
home/
home/abernal/
home/abernal/.bash_profileCompare file sizes
[root@rhel ~]# ls -lh /tmp/home.tar*
-rw-r--r--. 1 root root 40K Mar 24 09:00 /tmp/home.tar
-rw-r--r--. 1 root root 12K Mar 24 09:00 /tmp/home.tar.bz2
-rw-r--r--. 1 root root 14K Mar 24 09:00 /tmp/home.tar.gzThe uncompressed .tar is 40K. The gzip version is 14K and the bzip2 version is 12K — smaller but takes longer to create.
List contents without extracting
[root@rhel ~]# tar -tzvf /tmp/home.tar.gz
drwxr-xr-x root/root 0 2026-03-24 home/
drwx------ abernal/abernal 0 2026-03-24 home/abernal/Extract to a different directory
[root@rhel ~]# tar -xjvf /tmp/home.tar.bz2 -C /tmp
home/
home/abernal/
home/abernal/.bash_profile
[root@rhel ~]# ls /tmp/home/
abernalQuick Reference — Common tar Commands
# Create gzip archive
tar -czvf archive.tar.gz /path/to/dir
# Create bzip2 archive
tar -cjvf archive.tar.bz2 /path/to/dir
# List contents (gzip)
tar -tzvf archive.tar.gz
# List contents (bzip2)
tar -tjvf archive.tar.bz2
# Extract gzip archive (current dir)
tar -xzvf archive.tar.gz
# Extract bzip2 archive (specific dir)
tar -xjvf archive.tar.bz2 -C /destination
# Extract single file
tar -xzvf archive.tar.gz path/to/fileRHCSA Exam Tips
> EXAM TIP: Archiving and compression are tasks usually done together to produce smaller archive files. You will likely see this on the RHCSA exam.
Key things to remember for the exam:
tar -czvf→ create + gzip compresstar -cjvf→ create + bzip2 compresstar -xzvf→ extract gzip archivetar -xjvf→ extract bzip2 archive-C /path→ extract to a specific directorytarstrips the leading/by default — use-Pto override- The
-rand-uoptions do not work on compressed archives
Summary
| Tool | Creates | Extracts | Extension |
|------|---------|----------|-----------|
| gzip | gzip file | gunzip file.gz | .gz |
| bzip2 | bzip2 file | bunzip2 file.bz2 | .bz2 |
| tar (plain) | tar -cvf | tar -xvf | .tar |
| tar + gzip | tar -czvf | tar -xzvf | .tar.gz |
| tar + bzip2 | tar -cjvf | tar -xjvf | .tar.bz2 |
In the next article we will cover the vim editor — the essential text editing tool for every Linux sysadmin.
*Questions? Find me on X as @stackdeploy