vi, and a final challenge

INPUT · Slides

A test of skill

01 / 08

From here, no tools are handed over

The last lesson. Well done for coming this far.

This lesson teaches not one new command. Everything that appears you have already learnt.

What differs is the wording of the problems.

until now   "count them with `grep`"from now    "I want to know how many 404s there are"

Only what is wanted is written. Which tool, and how, you decide.

It may feel uneasy at first. But do remember this.

out in the field, nobody names the tool for you

You are asked to "count the 404s in the log". Nobody says "count them using grep -c".

That is why this lesson is here.

Let me hand you a knack for going about it.

1. put what you want into words2. recall the tool nearest to it3. try it small4. look at the result and mend it

Do not try to type the right answer straight off. Trying and mending is how a terminal is used.

The ways out when stuck are all in place too.

ls        see how things standcat       check the contentscommand --help   read how to use itEsc :q!   escape from vi

You have hands for checking. Stuck, you look.

Right, let us begin. Ten problems.

02 / 08

To help you recall — looking and finding

Let me show you the shelf of tools at a glance. Forgetting the fine detail is fine. If you recall "such a thing was possible", you look the rest up with --help.

First, the tools for looking.

ls        what is there (chapter 1)pwd       where you are (chapter 1)cat       the whole contents (chapter 3)head tail the head only, the end only (chapter 3)less      a long thing a little at a time (chapter 3)wc        count (chapter 3)

Next, the tools for finding.

find      look for files by name or kind (chapter 4)grep      look for lines that say it (chapter 5)

Hold on to the difference between the two.

find   look for files (the outside)grep   look inside files (the inside)

"Which file" is find, "which line" is grep.

grep is much used, so here are its shapes.

grep word file      the lines with that wordgrep -c word file   the number of lines with itgrep -v word file   the lines without itgrep -n word file   with line numbers

-c and -v come up especially often.

"how many are there?"   -c"what about the rest?"  -v

There are problems where these two appear. Keep them in a corner of your mind.

03 / 08

To help you recall — working and joining

The tools for working on what you fetched.

sort      put in order (chapter 5)uniq      gather up repeats (chapter 5)cut       cut out a column (chapter 5)tr        swap characters (chapter 5)sed       replace in lines (chapter 5)awk       reckon using columns (chapter 5)

And the tools for joining them.

|    the output on the left into the input on the right (chapter 4)>    write to a file (chapter 4)>>   add to a file (chapter 4)<    read from a file (chapter 4)

Joining with | is the terminal's greatest weapon.

cat log | grep 404 | wc -l

Line up small tools and do a big job. You need not make one tool do everything.

Recall the much-used combinations.

sort | uniq        remove repeatssort | uniq -c     count themsort | uniq -d     only the ones repeatedgrep word | wc -l  count (the same as -c)

The second is especially strong.

sort names.txt | uniq -c      3 sato      2 suzuki      1 tanaka

"Which came up how many times", at a glance. Used again and again when looking at logs.

why is the sort needed

Because uniq only sees identical lines next to each other. So you put them in order first. Remembering the reason too, you can build it again when you forget.

04 / 08

To help you recall — mending and running

The tools for making and mending files.

mkdir     make a directory (chapter 2)touch     make an empty file (chapter 2)cp mv rm  copy, move, remove (chapter 2)vi        mend the contents (chapter 13)echo >    write (chapter 4)

Do not forget the tools for permissions and processes.

chmod     put on the mark for running (chapter 6)ps kill   see what runs, stop it (chapter 8)./name    run a script (chapter 11)

And the tools of the shell.

variables   name=value and $name (chapter 10)for         repeat (chapter 11)if          branch (chapter 11)$?          did it go well (chapter 11)

for comes out for "the same thing many times".

for i in 1 2 3; do  echo "web$i"done

Faster than typing it three times by hand.

Let me write out the choice between vi and sed once more.

sed   replace them all (fast, unseen)vi    replace one at a time (slow, checkable)

Problems where either will do come up. If the answer fits, either is right.

a tool is a means, not an end

Reach the result you want and the road need not be one. So do not mind having typed something different from the model answer.

05 / 08

Three hands for when you are stuck

When a problem will not come, try these in order.

The first — see how things stand

lscat filepwd

Going on by assumption is the most dangerous thing. Look at the contents of a file and the next thing to do often shows itself.

The second — break it small and try

cat log | grep 404 | wc -l    (long)

If this will not run, try from the left in turn.

cat log                 (does it come out)cat log | grep 404      (does it narrow)cat log | grep 404 | wc -l

Pin down where it breaks. The same thinking as "read the message as it stands" in chapter 3.

The third — ask the tool

grep --helpwc --help

The tool knows how it is used. You need not remember.

When it still will not come, open a hint. The hints of this lesson do not name a tool; instead they say which chapter it was in.

"counting things was in chapter 3"

Only a thread to pull. Build the answer yourself. It sticks better that way.

06 / 08

Check, and then finish

In this lesson, the job runs as far as checking what you did.

mended -> checked -> done

What is not checked is not finished.

Let me hand you the shapes for checking.

made      see it is there with lsmended    see the contents with cat or grepcounted   count again another wayremoved   see it is not there with ls or grep

Checking "that it is not there" is the easy one to miss.

grep -c word file    0 and it is not therels file              an error and it is not there

Recall bracketing from two angles as well.

is what you want thereis what you do not want gone

One side alone will not do. Something this course has said many times.

replaced   the number changed + the number not leftremoved    what went + what stayedmade       that it is there + its contents

Look at both and mistakes show.

And the most important thing once more.

> Let the machine count

Counting by eye gets it wrong. There is wc, and grep -c. You are already someone who can set a machine to work.

07 / 08

Looking back over thirteen chapters

Before the last problems, let us look at the road you walked.

ch 1   where you are now (pwd cd ls)ch 2   make, copy, remove (mkdir cp mv rm)ch 3   read the contents (cat head tail less wc)ch 4   find and join (find | > <)ch 5   narrow and order (grep sort uniq cut sed awk)ch 6   permissions (chmod, how to read them)ch 7   users and groups (su useradd)ch 8   processes (ps top kill &)ch 9   the filesystem (df du ln tar)ch 10  the rules of the shell (variables, expansion, PATH)ch 11  scripts (#! for if $?)ch 12  keeping it running (cron logger ip ping httpd)ch 13  vi (Esc i, move, remove, copy, search)

Thirteen chapters, and all of that learnt.

Do you remember what was written in the first lesson?

the black screen is not frightening

And now?

type ls and you know what is theretype cat and you can read the contentsopen vi and Esc :q! always gets you out

There is less that you do not know. That is what "not frightening" means.

You will meet commands you do not know from here on. But you have the steps already.

read --helptry it smallcheck the result

With these three, any tool becomes usable. That is what I would most have you take away from thirteen chapters.

08 / 08

Right, the test of skill

Ten problems. They grow harder in turn.

1-3    one tool solves it4-7    join two or three8-10   think, and build

The last three may take a little time. That is fine. Work in the field does not finish at once either.

Let me hand you three attitudes.

Getting it wrong is allowed

typed it wrong  -> type it againremoved too much -> u or :q!do not know      -> open a hint

It is built so it will not break. Go at it boldly.

The answer is not one thing

grep -c 404 loggrep 404 log | wc -l

Both give the same number. Different from the model answer, but if the result fits it is right.

Not knowing, you may still go on

Stuck on one, you may go to the next. Come back later.

even solving none of them, you can use a terminal

Having walked thirteen chapters this far is itself the answer.

Right, let us begin. Your tools are in your hands.