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.
answers/2.1.yml~/notes.txtopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | Working with files |
|---|---|
| The log | files/logs/r1.log: r1’s own log, recorded from this lab’s router |
| A growing file | make live adds a line to files/logs/live.log every 3 seconds, for a minute |
| Your answers | answers/2.1.yml |
new to this? step by stepbeginner
head -n 1 files/logs/r1.logandtail -n 1 files/logs/r1.log: the first and last events.less files/logs/r1.log, type/GigabitEthernet1and Enter, n for the next match, q to quit.make live, thentail -f files/logs/live.log. Note the word. Ctrl+C stops watching.- Challenge:
nano ~/notes.txt, typechecked r1, Ctrl+O Enter to save, Ctrl+X to leave. - Fill in answers/2.1.yml, then
make check T=2.1.
more in meet the lab
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.
| Tool | Shows you | Good for |
|---|---|---|
cat file | the whole file at once | short files |
less file | the file a screen at a time, with search | anything longer than a screen |
head -n 5 file | the first 5 lines | what a file starts with |
tail -n 5 file | the last 5 lines | the newest entries in a log |
tail -f file | the last lines, then every new line as it’s written | watching 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.
| Key | Does |
|---|---|
| Space / b | a screen down / back up |
| ↓ ↑ | a line down / up |
/word then Enter | search forward for word; n is the next match, N the one before |
| g / G | the first / the last line |
| q | quit |
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.
| Keys | Does |
|---|---|
| Ctrl+O, then Enter | save (write Out) |
| Ctrl+X | leave. If you haven’t saved, it asks first |
| Ctrl+W | search |
| Ctrl+K / Ctrl+U | cut 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.
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:
| Part | Means |
|---|---|
*Nov 11 05:42:33.264 | when it happened |
%CAPEOS-3-LINK_DOWN | who said it (CAPEOS), how serious it is (3: the lower, the worse) and the event |
Interface GigabitEthernet2 is down ... | what happened, in words |
Read the log, then watch one grow
The first and the last line:
run in the terminalhead -n 1 files/logs/r1.log tail -n 1 files/logs/r1.logTheir events, the words after
%CAPEOS-5-, go infirst_eventandlast_event.Open the whole log in
lessand search it. Type/GigabitEthernet1and press Enter: less jumps to the first line that mentions it, and highlights it. Press n to see the next one.run in the terminalless files/logs/r1.logWhen did GigabitEthernet1 first go down (
LINK_DOWN)? Its time, like05:42:55, goes ingi1_down_time. Press q to come out.Now a log that’s still being written. Start the lab’s live feed, then watch it:
run in the terminalmake live tail -f files/logs/live.logA new line appears every 3 seconds. One of them says the word is …: that word goes in
live_word.tail -fkeeps watching until you stop it with Ctrl+C. It doesn’t change the file.Fill in the four lines:
editanswers/2.1.ymlCtrl+P → 2.1.ymlfirst_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 liveYour
gi1_down_timemay 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.
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.1when 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