Skip to main content

Archiving and Compressing

Hands-On Lab

 

Photo of Ross Brunson

Ross Brunson

Linux Training Architect II

Length

00:45:00

Difficulty

Beginner

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Archiving and Compressing

Introduction

Archiving files on Linux systems is an important system task, and the tar command is a foundational command to understand and use properly for archiving. Due to the increasing amount of storage that proper backups take, it's important to know what compression options and commands exist and how to use them either alone or in concert with tar. 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.

Solution

Log in to the lab server using the credentials provided:

ssh cloud_user@<PUBLIC IP ADDRESS>

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

  1. Check the size of the files we're looking to archive:

    du -sh /usr/share/doc/packages

    Note its size (around 100M).

  2. Create an archive:

    tar -cf packagedocs.tar /usr/share/doc/packages
  3. Run ls -l on the file:

    ls -l *.tar

    We should see the one we just created.

  4. Check its size:

    du -sh packagedocs.tar

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

  5. Let's look at the files in it:

    tar -tf packagedocs.tar

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

  6. Get more information:

    tar -tvf packagedocs.tar
  7. Extract the file:

    tar -xvf packagedocs.tar

    This will create a usr/ directory.

  8. Check the sizes:

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

    We should see that the sizes match.

  9. Compare the sizes:

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

    We'll see they're essentially the same.

  10. Create a compressed archive file with gzip:

    tar -czvf packagedocs.tar.gz /usr/share/doc/packages
  11. 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.)

  12. Create a compressed archive file with bzip:

    tar -cjvf packagedocs.tar.bz2 /usr/share/doc/packages
  13. 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).

  14. Create a compressed archive file with xz:

    tar -cJvf packagedocs.tar.Z /usr/share/doc/packages
  15. 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.

  16. Delete the directory:

    rm -rf ./usr
  17. Make sure it's gone:

    ls -ld ./usr

    We should see it's no longer there.

  18. List the contents of the .gz archive:

    tar -tzvf packagedocs.tar.gz
  19. Search for "wicked":

    tar -tzvf packagedocs.tar.gz | grep wicked

    This will show us everything that includes "wicked".

  20. Extract specific directory name:

    tar -xzvf packagedocs.tar.gz usr/share/doc/packages/wicked/samples
  21. 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.

  22. Extract the entire archive:

    tar -xzvf packagedocs.tar.gz
  23. 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

  1. Copy packagedocs.tar to gziptest.tar:

    cp packagedocs.tar gziptest.tar
  2. Copy packagedocs.tar to bzip2test.tar:

    cp packagedocs.tar bzip2test.tar
  3. Verify they're there:

    du -sh *zip*

    We should see them both.

  4. Compress gziptest.tar:

    gzip gziptest.tar
  5. Compress bzip2test.tar:

    bzip2 bzip2test.tar
  6. 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.

  7. 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.

  8. Instead, run the following:

    gunzip gziptest.tar.gz
  9. Now, do the same thing with bunzip2:

    bunzip2 bzip2test.tar.bz2
  10. See what's there:

    du -sh *zip*

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

  11. If you want to keep the original file, run:

    gzip -k gziptest.tar
  12. See what's there now:

    du -sh *zip*

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

Conclusion

Congratulations on successfully completing this hands-on lab!