- A Cloud Guru
Working with Compressed Files in Linux
Each candidate for the LPIC-1 or CompTIA Linux exam needs to understand how to work with various types of compressed files, or "tarballs" as they are commonly known. We will practice with various compression tools, and compare the differences between them.
Table of Contents
Try out different compression methods
Note: Please give the lab an extra minute or so before connecting via ssh to allow the lab to fully provision.
Take a look at the original size of your junk.txt file, and make note of it:
ls -lh junk.txt
First, let's try the gzip compression method. The following command will compress the junk.txt file using gzip:
Now, run the 'ls' command to view the size of the file:
Notice that the gzip command replaced the original file with a compressed version of it. The other compression commands we will use will do the same. Take note of the smaller size of the file. Then, decompress the gzip file to get the original junk file back:
Next, perform the same steps, using the bzip2 compression method:
Note that this compression method will take slightly longer than the previous. Make a note of the bzip2 file's size (typically, these file sizes are smaller than gzip compressed files):
ls -lh junk.txt.bz2
Once again, decompress the file to get the original back:
Now we will try out a newer compression method, using 'xz':
Note that this compression will take some time as well. Once the command completes, view your file's size:
And finally, decompress the file:
Create tar files using the different compression methods.
This next set of tasks will focus on working with tar files. First, use the gzip compression method to make a tarball:
tar -cvzf gztar.tar.gz junk.txt
Then, make a new tarball using bzip2:
tar -cvjf bztar.tar.bz2 junk.txt
Lastly, use xz to make a tarball:
tar -cvJf xztar.tar.xz junk.txt
Run the ls command again to compare the file sizes:
Notice that creating tar files did not replace the original junk.txt file. Note also how close in size the xz and bzip2 files are to each other.
Practice reading compressed text files.
The final group of tasks will demonstrate how to read compressed files, without decompressing them on your disk. First, copy over the /etc/passwd file to your home directory:
cp /etc/passwd .
Now, compress the file using bzip2 into a tarball:
tar -cvjf passwd.tar.bz2 passwd
Use the bzcat command to read the bzip2 compressed file:
Do the same for a gzipped tar file:
tar -cvzf passwd.tar.gz passwd
And use the zcat command to read this compressed file:
And finally, create an xz tar file:
tar -cvJf passwd.tar.xz passwd
And use the xzcat command to read its contents:
When done, hand the server over to be graded.
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.