Beginner 7 MIN READ

File Compression and Archiving with tar, gzip, and bzip2 in RHEL

Learn how to compress and archive files in RHEL using gzip, bzip2, and tar. Real lab examples with commands, options, and RHCSA exam tips included.

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:

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

Lab — Compressing a file with gzip

First, copy the /etc/fstab file to the root home directory and verify:

bash
[root@rhel ~]# cp /etc/fstab /root/fstab
[root@rhel ~]# ls -lh /root/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 fstab

Now compress with gzip and confirm:

bash
[root@rhel ~]# gzip fstab
[root@rhel ~]# ls -lh /root/fstab.gz
-rw-r--r--. 1 root root 401 Mar 24 09:00 fstab.gz

The original file is gone and replaced by fstab.gz. View compression info:

bash
[root@rhel ~]# gzip -l fstab.gz
         compressed        uncompressed  ratio uncompressed_name
                401                 617  35.0% fstab

Lab — Decompressing with gunzip

bash
[root@rhel ~]# gunzip fstab.gz
[root@rhel ~]# ls -lh /root/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 fstab

The 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

bash
[root@rhel ~]# bzip2 fstab
[root@rhel ~]# ls -lh /root/fstab.bz2
-rw-r--r--. 1 root root 385 Mar 24 09:00 fstab.bz2

Notice the .bz2 extension. The file is slightly smaller than the gzip version.

Lab — Decompressing with bunzip2

bash
[root@rhel ~]# bunzip2 fstab.bz2
[root@rhel ~]# ls -lh /root/fstab
-rw-r--r--. 1 root root 617 Mar 24 09:00 fstab

File 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

bash
[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_logout

The 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

bash
[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_profile

Archive specific files

bash
[root@rhel ~]# tar -cvf /tmp/files.tar /etc/fstab /etc/hosts
tar: Removing leading '/' from member names
etc/fstab
etc/hosts

Append files to an existing tarball

bash
[root@rhel ~]# tar -rvf /tmp/files.tar /etc/yum.repos.d/
etc/yum.repos.d/
etc/yum.repos.d/redhat.repo

Extract a single file from a tarball

bash
[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/fstab

Extract all files from a tarball

bash
[root@rhel ~]# tar -xvf /tmp/files.tar
etc/fstab
etc/hosts
etc/yum.repos.d/
etc/yum.repos.d/redhat.repo

Lab — 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

bash
[root@rhel ~]# tar -czvf /tmp/home.tar.gz /home
tar: Removing leading '/' from member names
home/
home/abernal/
home/abernal/.bash_profile

Create a bzip2-compressed tarball

bash
[root@rhel ~]# tar -cjvf /tmp/home.tar.bz2 /home
tar: Removing leading '/' from member names
home/
home/abernal/
home/abernal/.bash_profile

Compare file sizes

bash
[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.gz

The 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

bash
[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

bash
[root@rhel ~]# tar -xjvf /tmp/home.tar.bz2 -C /tmp
home/
home/abernal/
home/abernal/.bash_profile
 
[root@rhel ~]# ls /tmp/home/
abernal

Quick Reference — Common tar Commands

bash
# 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/file

RHCSA 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:


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

← Back to Articles
Written by Andres Bernal | @abernal093