Lab Exercises for Lesson 08 Intro: The lab for this lesson is an introduction to compression. To begin the lab, please u

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Lab Exercises for Lesson 08 Intro: The lab for this lesson is an introduction to compression. To begin the lab, please u

Post by answerhappygod »

Lab Exercises for Lesson 08
Intro:The lab for this lesson is an introduction to compression. To beginthe lab, please use the script command to record yoursession. Activity 1: bzip2, bunzip2, bzcatTo begin our compression lab, we'll need a compressible file withwhich we can experiment. Use the echo command to create afile containing several occurrences of the letter "e" by holdingdown the e key on your keyboard for several seconds:$ echo"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" > lotsofes.txtVerify the file exists with the ls command. We also needa copy of the the_raven.txt file we downloaded in Lesson 07. If youno longer have this file, you can download it again with the curlcommand:$ curl -Ohttps://www.deanza.edu/faculty/metcalfkevin/doc ... xtCompress the lotsofes.txt file with bzip2 then use ls tolist the contents of the directory:$ bzip2 lotsofes.txt$ ls -lNotice how the file lotsofes.txt is no longer in the directory; ithas been replaced by a file named lotsofes.txt.bz2. By default,the bzip2 command does NOT retain a copy of the file it'sreplacing. To retain a copy of the file, use the -k (keep)option:$ bzip2 -k the_raven.txt$ ls -lTo uncompress files that have been compressed using bzip2compression, you can use the bunzip2 command.Like bzip2, the bunzip2 command can take severaloptions which will affect its behavior. First, let's use the -coption to cause bunzip2 to dump the output of thedecompression to standard out; in other words, we're going to justdisplay the file contents without creating a copy of thedecompressed file on the disk. Run thefollowing bunzip2 command, followed by ls:$ bunzip2 -c lotsofes.txt.bz2$ ls -lNotice how the file displays to the screen, but does not actuallyget decompressed. Next, let's decompress the file, followed againby the ls command:$ bunzip2 lotsofes.txt.bz2$ ls -lAs you can see, like bzip2, the default behaviorof bunzip2 is to decompress the file *in place* - meaningthe command will not retain the original file. Another defaultbehavior of bzip2 is a refusal to overwrite a compressedfile if it already exists. For example, the_raven.txt.bz2 exists,so run the following command and note the error message youreceive.$ bzip2 -k the_raven.txtWe can override the behavior with the -f (force) option:$ bzip2 -k -f the_raven.txtSuppose we want to know how much space we've saved by compressingthis file. We can take the resulting file size and divide that bythe starting file size (1943 bytes / 6955 bytes), which will tellus the new file is around 27.94% the size of the original - meaningwe've reduced the file's size by 72.06%.However, bzip2 provides the -v (verbose) command lineoption - a handy shortcut for reporting various information -including the size reduction ratio post-compression:$ bzip2 -k -f -v the_raven.txtNote: when using a significant number of command line options tothe various utilities we're invoking, we can begin to save time by"bundling" our options together:$ bzip2 -kfv the_raven.txtbunzip2 also provides the -t (test) option, which is a handymethod for determining whether a compressed file has beencorrupted. However, as we'll see in the examples below, -t does notprovide output to the screen by default. To do so, we'll need toinvoke the -v (verbose option as well):$ bunzip2 -t the_raven.txt.bz2$ bunzip2 -tv the_raven.txt.bz2Finally, you may run across the -d (decompress) optionof bzip2. As mentioned in the man page,the bunzip2 command can also be invoked via the -d optionto bzip2. Meaning the following two commands areidentical:$ bzip2 -d the_raven.txt.bz2and$ bunzip2 the_raven.txt.bz2As you may have guessed by now, the bzcat command(covered in the lecture) is actually a shortcut to the -c optionto bzip2 -d. Meaning for a file compressed with:$ bzip2 lotsofes.txt... the following three commands are all equivalent:$ bzcat lotsofes.txt.bz2$ bzip2 -d -c lotsofes.txt.bz2$ bunzip2 -c lotsofes.txt.bz2Activity 2: gzip and gunzipNo discussion of compression on Linux would be complete withoutdiscussing gzip/gunzip. The gzip family of commandsare the predecessors to bzip2. As you download files for Linuxfrom the Internet, you'll quickly discover that severalorganizations distribute their content in .gz format. Sincethe bzip2 utilities borrow usage details fromthe gzip utils, you'll find the usageof gzip familiar:$ gzip the_raven.txt$ ls -lNotice that, like bzip2, gzip has also removed theoriginal file. Unlike bzip2, however, gzip does notprovide an equivalent to the -k option for keeping the original. Toaccomplish this you'll need to compress to standard out (-c) andthen redirect stdout to a file (>). Confused? Let's look at anexample. First, let's decompress the file we just compressed:$ gzip -d the_raven.txt.gzNext, let's compress to stdout with redirection anduse ls to see the output (which I'll include here):$ gzip -c the_raven.txt > the_raven.txt.gz$ ls -l-rw-rw-r--. 1 kmetcalf kmetcalf 6747 Jun 11 22:23the_raven.txt-rw-rw-r--. 1 kmetcalf kmetcalf 1785 Jun 11 22:23the_raven.txt.bz2-rw-rw-r--. 1 kmetcalf kmetcalf 1738 Jun 12 06:30the_raven.txt.gzNotice how gzip has actually generated a smaller filethan bzip2? For files that are small enough, this happensoccasionally. Now use the -c and > function to compress thelotsofes.txt file. If the file is not already uncompressed, youmight need to uncompress it with bunzip2 or bzip2-d.
Variants: On some versions of the gzip utility from Unix (suchas the one used by MacOS), you may find a -k option on gzip. Forthe purposes of this class, however, homework and tests will assumewe're using the GNU utility such as can be found on Voyager.Activity 3: tarOur final lab for this lesson will cover the tar command.View the man page for tar using the following command andskim through all the ways that the tar command can be used:$ man tarYou are not required to memorize all these options; the purpose ofthis exercise is to familiarize you with how many options (some ofwhich appear to accomplish the same things, like -A and -r) thereare for this command. Read through the entry for tar inthe textbook. The purpose of tar is not compression, butrather to combine several files into a single file - usuallyreferred to as a "tarball." Compressing the resulting tarball isaccomplished via options such as -z (gzip) or -j (bzip2). Run thefollowing command:$ tar -zcf alltext.tar.gz *.txtThis command will cause the tar command to create a file(-c) which combines all the .txt files in the local directory(*.txt), then compresses them all with gzip (-z), andthen saves the resulting file as alltext.tar.gz (-falltext.tar.gz). To view the contents of the compressed tarball,use the -t option. Since tar knows the file is compressedwith gzip, the -z option may be omitted:$ tar -ztf alltext.tar.gz$ tar -tf alltext.tar.gzWe can decompress all files with the the -x (expand) option. We canspecify that we do not wish to overwrite any files withdecompressed versions using the -k (keep) option. Finally, the -v(verbose) option can be used to report on every file thatthe tar command is processing. Note that the examplebelow may generate error messages since we still have the originalfiles:$ tar -vkzxf alltext.tar.gz
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply