Page 1 of 1

Lab Exercises for Lesson 07 Intro: The lab for this lesson will help you learn the basics of Linux filters. To begin the

Posted: Sun Jul 03, 2022 12:00 pm
by answerhappygod
Lab Exercises for Lesson 07
Intro:The lab for this lesson will help you learn the basics of Linuxfilters. To begin the lab, please use the script command to recordyour session. Activity 1: headThe head utility is a useful Linux filter that displays part of afile. To show how this works, let's first download a largish textfile using the curl command (Note: that's -O: capital Oh below).We'll follow it with ls -l so we can see the list of all files, andthen wc -l (the letter ell; not the number 1) to count the numberof lines in a file.$ curl -Ohttps://www.deanza.edu/faculty/metcalfkevin/doc ... _raven.txt$ ls -l$ wc -l the_raven.txtNow let's use the head utility on the file we've justdownloaded:$ head the_raven.txthead will display the first 10 lines of that file. head can beupdated to display an arbitrary number of lines:$ head -n 3 the_raven.txtUse the man command to look at the other options available to thehead command:$ man headNotice how you can combine several options in powerful ways. Forexample, you can view the first two lines of all files in thecurrent directory - suppressing header display - with the followingshort command:$ head -n 2 -q *Activity 2: tailSometimes referred to as the opposite of the head command, the tailfilter can be used to view the last pieces of a file:$ tail the_raven.txtView the man pages for tail and look at the other optionsavailable. Now run tail with the necessary options to view the last10 bytes of the_raven.txt. Hint: the last byte is likely a newlinecharacter, so you will probably only "see" 9 characters.Activity 3: grepOf all the filters available in Linux, probably the most frequentlyused is grep. The grep command searches provided input for linesthat match some specified pattern. For example, if I only want tosee information about ravens from the the_raven.txt file, I can doso using:$ grep raven the_raven.txtNotice how any lines that have the work "raven" (including"craven") are also returned? grep does not match the WORD we'vespecified, but rather the PATTERN. As a more advanced option, if wewant to display only "raven" but not "craven", we could usesomething like:$ grep [^c]raven the_raven.txtA thorough understanding of how grep works is outside the scope ofthis class, but later in the quarter we will be going over grep ina bit more detail.Activity 4: sortThe sort command is useful for arranging data alphabetically. Toview each line in our the_raven.txt file sorted alphabetically, wecould use something like:$ sort the_raven.txtNotice that this displays all 100+ lines to the screen, which iskind of difficult to read. Linux provies the pipe character (|) toredirect the output of one command into the input of another. Forexample, let's assume we only want to see the first ten lines ofour sorted the_raven.txt file. We can accomplish this easily usinga pipe:$ sort the_raven.txt | head
Notice how you only see a bunch of empty lines? A lineconsisting of only a single newline (return) character sorts beforeother text. Virtually all filters can be used in the same way. Runa command to view the LAST 10 lines of the sorted file. Finally,use the man page from sort to figure out how to sort in reverseorder, and then come up with a command that uses a pipe to displaythe first ten lines of the reverse-sorted file. Hint: the outputfrom this command will likely start with a line that begins withthe word "Wetch" and ends with line that begins with the word"Till" .Activity 5: uniqThe uniq command shows only unique data. First, let's createseveral identical lines in a file (note: there are TWO greater thansigns below - they are both required as this tells the shell toappend to the file):$ echo "hi" >> e.txtPress the up arrow key to quickly retype the previous command.Press enter again. Repeat this process 10 times, then cat the fileand see several lines in the file. If you do not see several linesin the file, be sure you've typed the lines above correctly.$ cat e.txtNow that we have a file with several duplicate lines, run the uniqcommand and see the difference:$ uniq e.txtPull up the man page for uniq and notice all the different optionsavailable. Use the -c option and show the number of times this lineappears in the file:$ uniq -c e.txtActivity 6: diffThe diff filter shows the difference between two files. To view howthe diff command works, let's create another file that is similarto the e.txt file. Repeat the following line 3 times:$ echo "hi" >> ee.txtNow, type:$ echo "hello" >> ee.txtFinally, repeat the first line another 6 times (don't forget youcan use the up and down arrows to find a previous command):$ echo "hi" >> ee.txtUse the cat command with the -n (show line numbers) option to seethe differences between the two files:$ cat -n e.txt$ cat -n ee.txtNow use the diff command to view only the differences:$ diff e.txt ee.txtNext, let's create two identical files and run diff againstthem:$ touch d1.txt d2.txt$ diff d1.txt d2.txtNotice that diff does not appear to do anything. The -s option fordiff can be used to force diff to report when files areidentical:$ diff -s d1.txt d2.txtActivity 7: moreThe more command is an older filter that can be used to display afile and pause at one page. While the use of more has mostly beensupplanted by less, it's important to know how the more commandworks and how it is different from less:$ more the_raven.txtPress the space bar to view the next page, then press the 'b' keyto go back one page. Note: This reverse navigation may not beavailable in all versions of Unix/Linux. To exit more, either pressthe q key or press the space bar several times until you get to theend. View the man page for more and read the "description" towardthe top. Notice the line about less.Activity 8: lessThe less command is a more advanced version of the more command.Note that less does everything more does, and more. Pretty muchevery Unix book will make some bad joke about this, so we'll bypassthat and just move on. Use the less command on our the_raven.txtfile:$ less the_raven.txtYou can use the space bar and 'b' key with less, but you can alsouse the up and down arrows to scroll through the file one line at atime. Press 'q' to quit out of the less utility. Once you havelearned the vim editor, you will be able to use more of theexpanded functionality of less.