- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
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.
Lab Info
Table of Contents
-
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/packagesNote its size (around 100M).
Create an archive:
tar -cf packagedocs.tar /usr/share/doc/packagesRun
ls -lon the file:ls -l *.tarWe should see the one we just created.
Check its size:
du -sh packagedocs.tarNote its size. It isn't too much smaller than the original.
Let's look at the files in it:
tar -tf packagedocs.tarNotice there isn't a
/at the beginning of any of them.Get more information:
tar -tvf packagedocs.tarExtract the file:
tar -xvf packagedocs.tarThis will create a
usr/directory.Check the sizes:
du -sh /usr/share/doc/packages ./usrWe should see that the sizes match.
Compare the sizes:
du -sh /usr/share/doc/packages/ ./usr packagedocs.tarWe'll see they're essentially the same.
Create a compressed archive file with
gzip:tar -czvf packagedocs.tar.gz /usr/share/doc/packagesCompare the sizes:
du -sh /usr/share/doc/packages/ ./usr packagedocs.*We should see the
.gzfile is significantly smaller. (It will be around 30M.)Create a compressed archive file with
bzip:tar -cjvf packagedocs.tar.bz2 /usr/share/doc/packagesCompare the sizes:
du -sh /usr/share/doc/packages/ ./usr packagedocs.*We should see the
.bz2file is only slightly smaller than the.gzfile (around 20M vs. 30M).Create a compressed archive file with
xz:tar -cJvf packagedocs.tar.Z /usr/share/doc/packagesCompare the sizes:
du -sh /usr/share/doc/packages/ ./usr packagedocs.*We should see the
.Zfile is ever-so slightly smaller than the.bz2file.Delete the directory:
rm -rf ./usrMake sure it's gone:
ls -ld ./usrWe should see it's no longer there.
List the contents of the
.gzarchive:tar -tzvf packagedocs.tar.gzSearch for "wicked":
tar -tzvf packagedocs.tar.gz | grep wickedThis will show us everything that includes "wicked".
Extract specific directory name:
tar -xzvf packagedocs.tar.gz usr/share/doc/packages/wicked/samplesVerify it happened:
tree -d ./usrNote: You may need to install
treeby runningsudo zypper install tree.We should see the tree showing it extracted the directory.
Extract the entire archive:
tar -xzvf packagedocs.tar.gzVerify it happened:
du -sh ./usrWe should see its size is around 100M.
-
Challenge
Use the Available Compression Utilities to Act on Files, Groups of Files, and Directories
Copy
packagedocs.tartogziptest.tar:cp packagedocs.tar gziptest.tarCopy
packagedocs.tartobzip2test.tar:cp packagedocs.tar bzip2test.tarVerify they're there:
du -sh *zip*We should see them both.
Compress
gziptest.tar:gzip gziptest.tarCompress
bzip2test.tar:bzip2 bzip2test.tarCheck what's there:
du -sh *zip*We should see there aren't any
.tarfiles, 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.gzWe should see a message saying it is not a
bzip2file and won't work.Instead, run the following:
gunzip gziptest.tar.gzNow, do the same thing with
bunzip2:bunzip2 bzip2test.tar.bz2See what's there:
du -sh *zip*We'll see the
.tarfiles are back, and they are the original, larger sizes.If you want to keep the original file, run:
gzip -k gziptest.tarSee what's there now:
du -sh *zip*We can see we kept the original file and have a compressed file.
About the author
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.