Viewing files

How do you read a file that is 8,000 lines long?

$ wc -l /var/log/dpkg.log18076 /var/log/dpkg.log$ head -2 /var/log/dpkg.log22026-10-24 14:05:10 startup archives install2026-10-24 14:05:10 install base-passwd:amd64 <none> 3.6.3build1$ tail -2 /var/log/dpkg.log32026-10-27 20:02:41 status half-configured fontconfig:amd64 2.15.0-1...2026-10-27 20:02:41 status installed fontconfig:amd64 2.15.0-1.1ubuntu2$ less /var/log/dpkg.log41wc -l: how long is it?look before you print 8,000 lines2head: the first lineswhat the file looks like3tail: the last linesin a log, the newest entries4less: one screen at a timeSpace, /text to search, qSettings and logs on Linux are plain text, so a few small commands read almost everything.cat file prints the whole file, which suits short ones such as /etc/hostname. For anything longer, use less: it does notload the whole file, so it opens a huge log at once. tail -f file stays open and prints each new line as it is written,which is how you watch a log while you test something. Ctrl-C stops it.

head and tail print the ends of a file, less pages through it, and tail -f follows a log as it grows.

Editors

With no desktop, what do you edit a file with?

GNU nano 7.2 /etc/login.defs# Password aging controls:## PASS_MAX_DAYS Maximum number of days a password may be used.# PASS_MIN_DAYS Minimum number of days allowed between password ...#PASS_MAX_DAYS 99999PASS_MIN_DAYS 0^GHelp^OWrite Out^WWhere Is^KCut^XExit^RRead File^\Replace^UPastenanoWhat you type goes into the file.The keys are listed at the bottom;^ means hold Ctrl.Ctrl-O EntersaveCtrl-XleaveCtrl-Wsearchvimon every server; works in modesA server has no desktop editor. nano works like one; vim is the one you can count on finding.nano is installed on Ubuntu and is the easier start. A minimal Red Hat system may carry only vi, and the Red Hat examexpects it, so learn enough vim to open a file, change a line, save and leave. The page below has exactly that.Programs that open an editor for you, such as crontab -e, use the one named in EDITOR.

nano is the simple terminal editor, with its keys listed on screen; vim is on every server and works in modes.

Text editors · Going deeper

Three modes, and the five keys that get you out of vim

Normal modekeys are commandsInsert modekeys are texti a oEscCommand line:w :q :wq :q!:Enterdd delete a line · yy p copy, paste/text search · u undo · gg G top, endvim starts in Normal mode, where every key is a command. Text goes in only in Insert mode.Press i to insert, Esc to come back, then :wq to write and quit or :q! to quit without saving. That is enough to edit anyconfiguration file on any server, which is why the Red Hat exam expects it. nano is simpler and shows its keys on screen;set EDITOR=nano if you prefer it, and commands such as crontab -e and visudo will open it instead.

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?

In /etc/ssh, to change one line of sshd_config:1 Copysudo cp -a sshd_config sshd_config.bakthe way back2 Editsudo nano sshd_configone change at a time3 Checksudo sshd -tsilent means the file is valid4 Applysudo systemctl reload sshthe service reads the file againSee exactly what changeddiff sshd_config.bak sshd_configUndosudo cp -a sshd_config.bak sshd_configthen check and apply againCopy, edit, check, apply. The copy makes the change reversible; the check catches a typing mistake.A running service does not notice an edited file until it is reloaded, so a mistake can sit unseen until the next restart.That is why the check comes before the reload. Many services have one: sshd -t, nginx -t, visudo -c, apachectl configtest.sudo runs one command as the administrator (lesson 6). On Red Hat systems the SSH service is named sshd, not ssh.

Copy the file, edit it, run the service’s configuration check, then reload: the copy and the check make the change safe.

Lab

Read a log, edit in nano and vim, and undo a change

$ cp ~/.bashrc ~/.bashrc.bak1$ nano ~/.bashrc2$ diff ~/.bashrc.bak ~/.bashrc3117a118> export EDITOR=nano$ cp ~/.bashrc.bak ~/.bashrc4$ diff ~/.bashrc.bak ~/.bashrc$ echo $?01Copy firstthe way back2Add one line at the endsave with Ctrl-O, leave with Ctrl-X3Read the changeline 118 was added4Undo itno difference: diff prints nothingLab: read a log, edit a file in nano and in vim, and undo a change.Six steps on your practice machine. Press the down arrow.

wc, head, tail and less read any file; a copy and diff make every edit reversible and visible.

Lab · Going deeper

Read, edit and undo

Everything here happens in your own home directory, so nothing can break the machine.

Steps

  1. 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.
  2. 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.
  3. 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.
  4. Edit in nano. cp ~/.bashrc ~/.bashrc.bak; nano ~/.bashrc; add export EDITOR=nano as the last line; Ctrl-O, Enter, Ctrl-X.
  5. 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.
  6. 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.

Lesson complete

You finished “Viewing and editing text”

Nicely done.

Open in a new tab