006 Archive Compress Unpack and Uncompress Files Using Tar Gzip and Bzip2

Tech Tutorial: Understand and Use Essential Tools #

Introduction #

In this tutorial, we will cover how to handle file archiving and compression on Red Hat Enterprise Linux (RHEL), a crucial skill for any system administrator preparing for the Red Hat Certified System Administrator (RHCSA) exam. We will focus on the use of tar, gzip, and bzip2. These tools are essential for managing file sizes and ensuring efficient use of disk space, which is vital in both everyday administration and in preparation for the exam.

Step-by-Step Guide #

1. Understanding the Tools #

  • tar: Stands for Tape Archive, and it is used to collect multiple files into a single archive file commonly known as a tarball.
  • gzip: GNU Zip, is used for file compression and decompression; it does not archive like tar, meaning it only compresses single files.
  • bzip2: Similar to gzip but provides better compression at the cost of speed.

2. Installing the Tools #

On RHEL, these tools are usually pre-installed. However, you can ensure they are installed and install them if they are not by using the following command:

sudo yum install tar gzip bzip2

Detailed Code Examples #

Creating an Archive Using tar #

To create a tar archive, use the following command:

tar -cvf archive_name.tar /path/to/directory_or_file
  • -c creates an archive.
  • -v verbosely lists the files processed (optional).
  • -f specifies the filename of the archive.

Example:

tar -cvf documents.tar /home/user/Documents

This command creates an archive called documents.tar from the contents of /home/user/Documents.

Extracting an Archive Using tar #

To extract a tar archive, use:

tar -xvf archive_name.tar
  • -x extracts files from an archive.

Example:

tar -xvf documents.tar

This command extracts the contents of documents.tar.

Compressing a File Using gzip #

To compress a file with gzip, simply run:

gzip filename

Example:

gzip example.txt

This replaces example.txt with example.txt.gz.

Decompressing a gzip File #

To decompress a file compressed with gzip, use:

gzip -d filename.gz

or

gunzip filename.gz

Example:

gzip -d example.txt.gz

This command decompresses example.txt.gz back to example.txt.

Compressing a File Using bzip2 #

bzip2 filename

Example:

bzip2 example.txt

This replaces example.txt with example.txt.bz2.

Decompressing a bzip2 File #

To decompress a file compressed with bzip2, use:

bzip2 -d filename.bz2

Example:

bzip2 -d example.txt.bz2

This command decompresses example.txt.bz2 back to example.txt.

Combining tar with Compression #

You can directly compress while creating a tar archive using the following commands:

  • For gzip: tar -czvf archive_name.tar.gz /path/to/directory
  • For bzip2: tar -cjvf archive_name.tar.bz2 /path/to/directory

Example with gzip:

tar -czvf documents.tar.gz /home/user/Documents

Example with bzip2:

tar -cjvf documents.tar.bz2 /home/user/Documents

These commands create a compressed archive in one step, which is more efficient and faster.

Conclusion #

Mastering the use of tar, gzip, and bzip2 is essential for efficient file management on RHEL systems. These tools not only help in reducing disk space usage but are also crucial for data archiving and sharing. Understanding and using these tools effectively will greatly assist any system administrator in managing files and preparing for the RHCSA exam. Make sure to practice these commands to become proficient in their use.