Viewing files
How do you read a file that is 8,000 lines long?
head and tail print the ends of a file, less pages through it, and tail -f follows a log as it grows.
Almost everything a server knows is in text files: its settings, and its logs. So how do you read one that is thousands of lines long? First ask how long it is, with w c dash l. head prints the first lines, enough to see what the file looks like. tail prints the last lines, and in a log those are the newest. less opens the file one screen at a time: Space for the next screen, slash to search, q to quit.
Editors
With no desktop, what do you edit a file with?
nano is the simple terminal editor, with its keys listed on screen; vim is on every server and works in modes.
A server has no desktop, so files are edited in the terminal. nano behaves the way you expect: what you type goes into the file. Its keys are listed along the bottom, and the caret means hold Control. Three are enough: Control O saves, Control X leaves, Control W searches. The other editor is vim. It is on every server, and it works in modes, which surprises everyone the first time. Press the down arrow for the few keys you need.
Text editors · Going deeper
Three modes, and the five keys that get you out of vim
In vim, Normal mode takes commands, i enters Insert mode, Esc leaves it, and :wq saves and quits.
Changing settings safely
How do you change a settings file without locking yourself out?
Copy the file, edit it, run the service’s configuration check, then reload: the copy and the check make the change safe.
Editing a settings file on a live server is where a typing mistake becomes an outage. Four steps make it safe. Copy the file first, so there is a way back. Edit, one change at a time. Check: most services can test their settings file without applying it, and silence means it is valid. Only then tell the service to read the file again. diff shows exactly what changed, and copying the backup over the file undoes it.
Lab
Read a log, edit in nano and vim, and undo a change
wc, head, tail and less read any file; a copy and diff make every edit reversible and visible.
Now do it yourself. In this lab you measure and page through a log, follow a file as it grows, edit a file in nano, see the change with diff and undo it, and then make and discard an edit in vim. Press the down arrow for the steps.
Lab · Going deeper
Read, edit and undo
Everything here happens in your own home directory, so nothing can break the machine.
Everything here happens in your own home directory, so nothing can break the machine. Steps. Measure before you print. Pick a log: /var/log/dpkg.log on Ubuntu, /var/log/dnf.log on Red Hat systems. Run wc -l on it, then head -5 and tail -5. Page through it. Open it with less. Search with /install, jump to the next match with n, go to the end with G and quit with q. Follow a file. In one terminal run tail -f ~/test.log. In a second, run echo "line one" >> ~/test.log a few times and watch them arrive. Stop with Ctrl-C. Edit in nano. cp ~/.bashrc ~/.bashrc.bak; nano ~/.bashrc; add export EDITOR=nano as the last line; Ctrl-O, Enter, Ctrl-X. See it and undo it. diff ~/.bashrc.bak ~/.bashrc shows the one line. Copy the backup over the file and run diff again: no output means identical. Edit in vim. vim ~/notes.txt: press i, type a line, press Esc, type :wq. Reopen it, delete the line with dd, and leave without saving with :q!. Check: You can open a file in vim, change it, and get out again, and diff shows you exactly what an edit changed.
Steps
- Measure before you print. Pick a log: /var/log/dpkg.log on Ubuntu, /var/log/dnf.log on Red Hat systems. Run wc -l on it, then head -5 and tail -5.
- Page through it. Open it with less. Search with /install, jump to the next match with n, go to the end with G and quit with q.
- Follow a file. In one terminal run tail -f ~/test.log. In a second, run echo "line one" >> ~/test.log a few times and watch them arrive. Stop with Ctrl-C.
- Edit in nano. cp ~/.bashrc ~/.bashrc.bak; nano ~/.bashrc; add export EDITOR=nano as the last line; Ctrl-O, Enter, Ctrl-X.
- See it and undo it. diff ~/.bashrc.bak ~/.bashrc shows the one line. Copy the backup over the file and run diff again: no output means identical.
- Edit in vim. vim ~/notes.txt: press i, type a line, press Esc, type :wq. Reopen it, delete the line with dd, and leave without saving with :q!.
Lesson complete
You finished “Viewing and editing text”
Nicely done.