Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon

Archiving and Compressing

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.

Google Cloud Platform icon

Path Info

Clock icon Beginner
Clock icon 45m
Clock icon Nov 27, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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.

  2. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans