Archiving and Compressing

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

In this lab, we’ll create tar archives from various sources, and then use the compression tools to create compressed versions of those archives, comparing compression algorithms’ apparent merits. We’ll also investigate the contents of compressed archives as well as uncompress them back to disk, either in their entirety or extracting a file from a compressed archive.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Use the `tar` Command to Create an Archive, View It, and Extract It

Check the size of the files we’re looking to archive:

du -sh /usr/share/doc/packages

Note its size (around 100M).

Create an archive:

tar -cf packagedocs.tar /usr/share/doc/packages

Run ls -l on the file:

ls -l *.tar

We should see the one we just created.

Check its size:

du -sh packagedocs.tar

Note its size. It isn’t too much smaller than the original.

Let’s look at the files in it:

tar -tf packagedocs.tar

Notice there isn’t a / at the beginning of any of them.

Get more information:

tar -tvf packagedocs.tar

Extract the file:

tar -xvf packagedocs.tar

This will create a usr/ directory.

Check the sizes:

du -sh /usr/share/doc/packages ./usr

We should see that the sizes match.

Compare the sizes:

du -sh /usr/share/doc/packages/ ./usr packagedocs.tar

We’ll see they’re essentially the same.

Create a compressed archive file with gzip:

tar -czvf packagedocs.tar.gz /usr/share/doc/packages

Compare the sizes:

du -sh /usr/share/doc/packages/ ./usr packagedocs.*

We should see the .gz file is significantly smaller. (It will be around 30M.)

Create a compressed archive file with bzip:

tar -cjvf packagedocs.tar.bz2 /usr/share/doc/packages

Compare the sizes:

du -sh /usr/share/doc/packages/ ./usr packagedocs.*

We should see the .bz2 file is only slightly smaller than the .gz file (around 20M vs. 30M).

Create a compressed archive file with xz:

tar -cJvf packagedocs.tar.Z /usr/share/doc/packages

Compare the sizes:

du -sh /usr/share/doc/packages/ ./usr packagedocs.*

We should see the .Z file is ever-so slightly smaller than the .bz2 file.

Delete the directory:

rm -rf ./usr

Make sure it’s gone:

ls -ld ./usr

We should see it’s no longer there.

List the contents of the .gz archive:

tar -tzvf packagedocs.tar.gz

Search for "wicked":

tar -tzvf packagedocs.tar.gz | grep wicked

This will show us everything that includes "wicked".

Extract specific directory name:

tar -xzvf packagedocs.tar.gz usr/share/doc/packages/wicked/samples

Verify it happened:

tree -d ./usr

Note: You may need to install tree by running sudo zypper install tree.

We should see the tree showing it extracted the directory.

Extract the entire archive:

tar -xzvf packagedocs.tar.gz

Verify it happened:

du -sh ./usr

We should see its size is around 100M.

Use the Available Compression Utilities to Act on Files, Groups of Files, and Directories

Copy packagedocs.tar to gziptest.tar:

cp packagedocs.tar gziptest.tar

Copy packagedocs.tar to bzip2test.tar:

cp packagedocs.tar bzip2test.tar

Verify they’re there:

du -sh *zip*

We should see them both.

Compress gziptest.tar:

gzip gziptest.tar

Compress bzip2test.tar:

bzip2 bzip2test.tar

Check what’s there:

du -sh *zip*

We should see there aren’t any .tar files, and both files there are significantly smaller than they were before.

Now, let’s use the wrong utility on the right file:

bunzip2 gziptest.tar.gz

We should see a message saying it is not a bzip2 file and won’t work.

Instead, run the following:

gunzip gziptest.tar.gz

Now, do the same thing with bunzip2:

bunzip2 bzip2test.tar.bz2

See what’s there:

du -sh *zip*

We’ll see the .tar files are back, and they are the original, larger sizes.

If you want to keep the original file, run:

gzip -k gziptest.tar

See what’s there now:

du -sh *zip*

We can see we kept the original file and have a compressed file.

Additional Resources

You've been tasked with analyzing the archiving and compression tools available on your SUSE Linux Enterprise Server system. The goal is to understand the tar command, gzip, bzip2, and xz, as well as their uncompression utilities such that you can conduct comparison tests between the various compression methods.

Log in to the lab server using the credentials provided:

ssh cloud_user@<PUBLIC IP ADDRESS>

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?