Linux Hands-On Lab - Working with Compressed Files in Linux
Linux Learning Path for Cloud and DevOps Engineers
📝Introduction
This post explains how a Linux system administrator needs to know 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.
📝Log in to the AWS Management Console
Using your credentials, make sure you're using the right Region. In my case, I am using AWS as my cloud provider and chose us-east-1
. However, you can choose any cloud provider and create your Linux Server VM (I am using a CentOS 7 distro) for this hands-on lab.
📌Note: You must create the AWS Access Key and AWS Secret Access Key and configure the AWS CLI in the terminal to use it.
You can use link1 and link2 for it.
📝Get the Original File Size and Create Zip Files
Let's take a look at the original size of any file and make note of it:
ls -lh
Gzip
First, let's try compressing with Gzip. The following command will compress a file using gzip
:
gzip <file>
Now, run ls
to view the size of the file:
ls -lh
Notice that the gzip
command replaced the original file with a compressed version of it. The other compression commands we use will do the same. Take note of the smaller size of the file, and then decompress it to get the original back:
gunzip <file>
Bzip
Now we're going to perform the same steps, but using the bzip2
compression method instead:
bzip2 <file>
Note this compression method will take slightly longer than the previous one. Let's check the resulting file size to see how it compares to using gzip
:
ls -lh <file>.bz2
It should be smaller than the gz
file.
Once again, decompress the file to get the original back:
bunzip2 <file>.bz2
XZ
Now we will try out a newer compression method, XZ
. It works with the same syntax as the others:
xz <file>
Note that this compression will take some time as well. Once the command completes, view your file's size:
ls -lh <file>.xz
The resulting file is about the same size as the last one. Now, like we did with the others, let's decompress the file:
unxz <file>.xz
📝Creating tar
Files
Next, we'll focus on working with tar
files. First, we're going to use Gzip
to make a tarball:
tar -cvzf gztar.tar.gz <file>
Then, let's make one using bzip2
:
tar -cvjf bztar.tar.bz2 <file>
Finally, we'll use XZ
to make one:
tar -cvJf xztar.tar.xz <file>
Where tar
options for the gzip
or bzip2
command are as follows:
-c : Create a new archive
-v : Verbose output
-f file.tar.gz : Use archive file
-z : Filter the archive through gzip
-j : Filter the archive through bzip2
Where tar
options for the xz
command are as follows:
-c : Create a new tar archive
-v : Verbose output
-J : Use the xz compression by calling the xz command
-f : Archive name
Run the ls
command again to compare the file sizes:
ls -lh
Notice that creating tar files did not replace the original file. Note also how close in size the xz
and bzip2
files are to each other.
📝Practice Reading Compressed Text Files
Reading the contents of compressed files without having to decompress them.
First, let's copy over the /etc/passwd
file to your home directory:
cp /etc/passwd /<dest_dir>
Gzip
We can do the same for a tar file, compressing it with Gzip
:
tar -cvzf passwd.tar.gz passwd
And we can use the zcat
command to read this compressed file:
zcat passwd.tar.gz
Bzip2
Now let's compress the file, using bzip2
, into a tarball:
[cloud_user@host]$ tar -cvjf passwd.tar.bz2 passwd
We can use the bzcat
command to read the compressed file:
bzcat passwd.tar.bz2
XZ
Finally, let's create an xz tar file:
tar -cvJf passwd.tar.xz passwd
And we can use the xzcat
command to read its contents:
xzcat passwd.tar.xz
📌Note - At the end of each hands-on Lab, always clean up all the resources previously created to avoid being charged if you used a Cloud Provider to provision them.
Congratulations — you have completed this hands-on lab covering the basics of working with Compressed Files in Linux.
Thank you for reading. I hope you understood and learned something helpful from my blog.
Please follow me on CloudDevOpsToLearn and LinkedIn, franciscojblsouza