online posts 16
linux and the command line 101

task 2.1 · chapter 2 of 8: working with files · 25 min

Read and write

objective

Read a real router log with less, head and tail, search it, watch a file grow with tail -f, then write a note in your home folder with nano.

you edit, in the labanswers/2.1.yml~/notes.txt

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonWorking with files
The logfiles/logs/r1.log: r1’s own log, recorded from this lab’s router
A growing filemake live adds a line to files/logs/live.log every 3 seconds, for a minute
Your answersanswers/2.1.yml
new to this? step by stepbeginner
  1. head -n 1 files/logs/r1.log and tail -n 1 files/logs/r1.log: the first and last events.
  2. less files/logs/r1.log, type /GigabitEthernet1 and Enter, n for the next match, q to quit.
  3. make live, then tail -f files/logs/live.log. Note the word. Ctrl+C stops watching.
  4. Challenge: nano ~/notes.txt, type checked r1, Ctrl+O Enter to save, Ctrl+X to leave.
  5. Fill in answers/2.1.yml, then make check T=2.1.

more in meet the lab

1 explain

Text all the way down

Logs and configs are text

Almost everything you read on a Linux machine or a router is a text file: lines of characters you can read with your eyes. Configs, logs, scripts, the inventory an Ansible playbook reads. That’s why a few small tools that read text get you so far.

ToolShows youGood for
cat filethe whole file at onceshort files
less filethe file a screen at a time, with searchanything longer than a screen
head -n 5 filethe first 5 lineswhat a file starts with
tail -n 5 filethe last 5 linesthe newest entries in a log
tail -f filethe last lines, then every new line as it’s writtenwatching a log live

in one sentence cat for short files, less for long ones, head and tail for the ends, tail -f to watch one grow.

less: the keys you need

less opens a file and waits for keys. It never changes the file.

KeyDoes
Space / ba screen down / back up
↓ ↑a line down / up
/word then Entersearch forward for word; n is the next match, N the one before
g / Gthe first / the last line
qquit

Editing in a terminal

In the codespace you can open any file in VS Code. On a server you reach with SSH, there’s no VS Code, so you need an editor that runs in the terminal. nano is the friendliest: it shows its own keys along the bottom. ^O means Ctrl+O.

KeysDoes
Ctrl+O, then Entersave (write Out)
Ctrl+Xleave. If you haven’t saved, it asks first
Ctrl+Wsearch
Ctrl+K / Ctrl+Ucut a line / paste it

Landed in vim by accident (a screen of ~ lines that ignores your typing)? Press Esc, type :q! and Enter: that quits without saving.

in one sentence nano is a terminal editor that lists its own keys: Ctrl+O saves, Ctrl+X leaves.

2 show

r1’s log

The top and the bottom of files/logs/r1.log, a log recorded from this lab’s router:

$ head -n 3 files/logs/r1.log
*Nov 11 05:42:28.266: %CAPEOS-5-RESTART: CapeOS 0.11.0 started
*Nov 11 05:42:32.023: %CAPEOS-5-LOGIN_OK: Login by admin from 172.80.102.1 port 60330
*Nov 11 05:42:33.264: %CAPEOS-3-LINK_DOWN: Interface GigabitEthernet2 is down (administratively)
$ tail -n 2 files/logs/r1.log
*Nov 11 05:44:03.607: %CAPEOS-5-LOGIN_OK: Login by admin from 172.80.102.1 port 35784
*Nov 11 05:44:04.722: %CAPEOS-5-CONFIG_DONE: Configured from console by root

Every line has the same shape, which is what makes logs easy to search:

PartMeans
*Nov 11 05:42:33.264when it happened
%CAPEOS-3-LINK_DOWNwho said it (CAPEOS), how serious it is (3: the lower, the worse) and the event
Interface GigabitEthernet2 is down ...what happened, in words
3 follow along

Read the log, then watch one grow

  1. The first and the last line:

    run in the terminal
    head -n 1 files/logs/r1.log
    tail -n 1 files/logs/r1.log
    

    Their events, the words after %CAPEOS-5-, go in first_event and last_event.

  2. Open the whole log in less and search it. Type /GigabitEthernet1 and press Enter: less jumps to the first line that mentions it, and highlights it. Press n to see the next one.

    run in the terminal
    less files/logs/r1.log
    

    When did GigabitEthernet1 first go down (LINK_DOWN)? Its time, like 05:42:55, goes in gi1_down_time. Press q to come out.

  3. Now a log that’s still being written. Start the lab’s live feed, then watch it:

    run in the terminal
    make live
    tail -f files/logs/live.log
    

    A new line appears every 3 seconds. One of them says the word is …: that word goes in live_word. tail -f keeps watching until you stop it with Ctrl+C. It doesn’t change the file.

  4. Fill in the four lines:

    editanswers/2.1.ymlCtrl+P → 2.1.yml
    first_event: RESTART
    last_event: CONFIG_DONE
    gi1_down_time: 05:42:55
    live_word: canyon      # yours is different: the lab picks it when you run make live
    

    Your gi1_down_time may differ if your copy of the log was recorded again; read it from yours.

in one sentence head and tail read the ends, less searches with /, tail -f follows a file as it grows.

4 challenge

A note in nano

Write a file called notes.txt in your home folder with nano, with one line in it: checked r1. Then cat it to make sure it saved, and run make check T=2.1.

hint

nano ~/notes.txt opens it, and makes it if it isn’t there yet. Type the line.

hint

Saving is Ctrl+O, and nano then asks for the file name with the name already filled in: press Enter. Ctrl+X leaves.

show the solution
nano ~/notes.txt          # type: checked r1, then Ctrl+O, Enter, Ctrl+X
cat ~/notes.txt
checked r1

nano works the same on every Linux server you’ll SSH into, including ws1 and srv1 in chapter 8. The codespace also has VS Code, and code ~/notes.txt opens the same file there.

5 check: run it, then prove it

$ make check T=2.1

when it's right, you'll see

╭─ task 2.1 · read and write
│  ✓ the first line's event (follow along): RESTART
│  ✓ the last line's event (follow along): CONFIG_DONE
│  ✓ when GigabitEthernet1 first went down (follow along): ...
│  ✓ the word that appeared in live.log (follow along): ...
│  ✓ ~/notes.txt, written in nano, says checked r1 (challenge)
╰─ 5/5 checks passed  verified