Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

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
Lab platform
Lab Info
Level
Beginner
Last updated
Sep 09, 2025
Duration
45m

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.

About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight