3.4. Reading and Editing Text

Configuration, logs, and documentation on Debian are text. The tools in this section are the ones an administrator reads with hourly: viewers for files and growing logs, two editors, the search-and-compare family, and the encoding and line-ending repairs that occasionally save a whole afternoon.

3.4.1. Viewing Files and Following Growing Output

Leave the links directory and work from the workspace root:

$ cd ~/debian-handbook/shell-basics

Create a small file to look at:

$ printf "one\ntwo\nthree\n" > view.txt

cat prints a file whole. With -n it numbers the lines, which makes it a quick way to cite a location in a configuration file:

$ cat -n view.txt
     1  one
     2  two
     3  three

Long files are better served by the selectors. head and tail print the first or last lines of a file:

$ head -n 2 view.txt
one
two
$ tail -n 2 view.txt
two
three

wc counts - lines, words, and bytes, in that order, with the file name last:

$ wc -l /home/john/debian-handbook/shell-basics/view.txt
3 /home/john/debian-handbook/shell-basics/view.txt

nl numbers like cat -n but with more control over the format; -ba numbers all lines including empty ones:

$ nl -ba view.txt
     1  one
     2  two
     3  three
head / tail option Effect
-n N Print the first / last N lines (default 10).
-f Keep the file open and print lines as they appear (tail).
-n +N with -f Start following from line N so history is included.

For files taller than the screen, less pages: it fills the terminal and waits. Space moves forward a screen, b back, /text searches forward, n repeats the search, and q quits. With -F, less quits immediately when the file fits on one screen, behaving like cat for small files:

$ less -F view.txt
one
two
three

Log watching is the same tool in continuous mode. tail -f prints the last lines and then follows: the file stays open, and every line appended by whatever is writing the log appears as it is written. Set up a log in its own directory:

$ mkdir -p follow && cd follow
$ : > app.log

: is a shell no-op, and > app.log truncates the file to zero bytes - creating it empty. In one terminal start the follower; it prints the last ten lines (there are none yet) and then waits:

$ tail -f app.log

In a second terminal, change to the same directory and append a line:

$ cd ~/debian-handbook/shell-basics/follow
$ printf "first line\n" >> app.log

And one more:

$ printf "second line\n" >> app.log

The first terminal shows each line as it arrives:

first line
second line

tail -f is stopped with Ctrl+C - one of the few places where interrupting a foreground command is the normal way out. Rotation is the classic pitfall: when a service renames and recreates its log, plain -f keeps reading the old file. tail -F reopens the file by name and picks up the new one; the systemd journal alternative is covered in Chapter 8.

3.4.2. Editing Configuration with a Terminal Editor

Debian ships two terminal editors by default, and they represent two different answers to the same question - how to edit text without a mouse:

Editor Style Where it comes from Trade-off
nano Modeless; shortcuts listed on screen Default installation (priority important) Gentle learning curve, fewer features
vi (vim-tiny) Modal: separate command and insert modes Default installation (priority important) Steeper start; vi exists on essentially every Unix system
vim (full) vim-tiny plus syntax, plugins Package vim Larger install, richer editing and highlighting

Both are present on the example system:

$ vi --version | head -n 1
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Nov 04 2026 22:12:34)
$ nano --version | head -n 1
 GNU nano, version 8.4

The vi on a default Debian installation is provided by the small vim-tiny package; installing vim upgrades it to the full editor with syntax highlighting.

Take a configuration file through a real edit. Back in the workspace root, create a directory and a small configuration file:

$ cd ~/debian-handbook/shell-basics
$ mkdir -p edit
$ printf "port=8080\nhost=127.0.0.1\n" > edit/app.conf

vi can apply an edit non-interactively in ex mode, which is also the honest way to show the change in a transcript - the flags below load no configuration, apply the substitution %s/8080/9090/ to every line, and write the file back:

$ vi -Nu NONE -n -es -c '%s/8080/9090/' -c 'wq' edit/app.conf

The command prints nothing. Read the file back:

$ cat edit/app.conf
port=9090
host=127.0.0.1

Interactively, the same edit is: open the file with vi edit/app.conf, move with the arrow keys, press i to enter insert mode and type, press Esc to return to command mode, then :wq to write and quit, or :q! to abandon changes. The modal distinction is the whole learning curve of vi - if the editor seems to ignore the keyboard, you are in the wrong mode, and Esc returns to command mode.

nano needs no modes: open the file with nano edit/app.conf, type directly, and follow the bar at the bottom of the screen - Ctrl+O then Enter writes the file, Ctrl+X exits, and Ctrl+K deletes a line. Either editor edits any text file; the choice is habit, not capability.

Editing a system configuration file adds two rules. First, make a backup copy before the first change, next to the original, with a name that says when and why - the .before suffix used here is one common convention:

$ sudo cp /etc/ssh/sshd_config /root/sshd_config.before

The copy is silent. Confirm it exists and is not empty:

$ sudo test -s /root/sshd_config.before && echo copied
copied

Second, edit the file with privilege and without root-owned editor state: sudoedit file makes a temporary copy owned by you, opens your editor on that copy with your own environment, and writes the file back only if it changed - or sudo vi file / sudo nano file for the same effect with fewer safety nets, since the editor then runs as root. When the experiment ends, remove the backup so the next reader is not puzzled by a stale copy:

$ sudo rm -f /root/sshd_config.before

One file must never be edited directly: sudoers, for the reasons Section 3.1.2 gave. Chapter 8 picks up the second half of the workflow - reloading or restarting the service so the edited configuration takes effect.

3.4.3. Searching Text and Comparing Files

grep selects the lines that match a pattern. Give it a log with both capitalized and lowercase markers - create one in a text directory:

$ mkdir -p text && cd text
$ printf "2026-11-17 ERROR disk full\n2026-11-17 INFO started\n2026-11-18 error network\n" > app.log

-n prefixes each match with its line number, which is what you quote in a report:

$ grep -n "ERROR" app.log
1:2026-11-17 ERROR disk full

-i ignores letter case, and combines with -n:

$ grep -in "error" app.log
1:2026-11-17 ERROR disk full
3:2026-11-18 error network

-c counts matching lines instead of printing them:

$ grep -c "ERROR" app.log
1

-q prints nothing and answers by exit status alone - the form scripts use:

$ grep -q "INFO" app.log; echo "status=$?"
status=0
$ grep -q "WARN" app.log; echo "status=$?"
status=1
grep option Effect
-i Ignore letter case.
-n Prefix matches with line numbers.
-c Print the count of matching lines.
-q Quiet: report by exit status only.
-r Recurse into directories.
-v Print the lines that do not match.

When a name in the list is missing, grep says so on standard error, reports it in its exit status, and still searches the files that do exist:

$ grep "x" /home/john/debian-handbook/shell-basics/text/no-such /home/john/debian-handbook/shell-basics/text/app.log; echo "status=$?"
grep: /home/john/debian-handbook/shell-basics/text/no-such: No such file or directory
status=2

Nothing matched the pattern x in app.log, so the only output is the error. Recursive search over a tree is the standard "where is this set?" question, with file name and line number in every hit:

$ grep -rn "error" /home/john/debian-handbook/shell-basics/text
/home/john/debian-handbook/shell-basics/text/app.log:3:2026-11-18 error network

Comparing files is a separate family. Two short files with one differing line:

$ printf "alpha\nbeta\n" > a.txt
$ printf "alpha\ngamma\n" > b.txt

diff -u prints a unified diff - the form patches and code reviews use. Its exit status follows its own convention: 0 for identical files, 1 for differences, 2 for trouble:

$ diff -u a.txt b.txt; echo "status=$?"
--- a.txt   2026-11-18 08:18:42.941268148 +0000
+++ b.txt   2026-11-18 08:18:42.941268148 +0000
@@ -1,2 +1,2 @@
 alpha
-beta
+gamma
status=1

The @@ line gives the position, - lines are in the first file only, + lines in the second. cmp stops at the first differing byte and says where; with -s it says nothing and leaves the answer in the status:

$ cmp -s a.txt b.txt; echo "status=$?"
status=1

comm compares two sorted files line by line and prints three columns: lines only in the first file, lines only in the second, and common lines. -12 suppresses the first two, leaving exactly the common lines. Both inputs must be sorted, so the example sorts them on the way in with process substitution - the <(command) form feeds a command's output as if it were a file:

$ comm -12 <(sort a.txt) <(sort b.txt)
alpha
Tool Question it answers
diff -u What changed, in a form people can read and patch with?
cmp -s Are these two files byte-identical?
comm -12 Which lines appear in both sorted files?

3.4.4. Encoding, Line Endings, and Locale

Debian works in UTF-8 by default, and almost everything you encounter will be UTF-8 - but files arrive from other systems, and the two classic surprises are a legacy single-byte encoding and Windows line endings. Both are diagnosable in bytes and repairable with standard tools.

Write a file containing the word café. From the workspace root, the mkdir makes a directory for the encoding examples, and the printf uses octal escapes to name the two bytes UTF-8 uses for é explicitly:

$ cd ~/debian-handbook/shell-basics
$ mkdir -p enc
$ printf "caf\303\251\n" > enc/utf8.txt

The file command identifies a file by its content, not its name:

$ file /home/john/debian-handbook/shell-basics/enc/utf8.txt
/home/john/debian-handbook/shell-basics/enc/utf8.txt: Unicode text, UTF-8 text

Look at the raw bytes with od - -An suppresses the byte offsets and -tx1 prints each byte in hexadecimal:

$ od -An -tx1 /home/john/debian-handbook/shell-basics/enc/utf8.txt
 63 61 66 c3 a9 0a

63 61 66 are c, a, f; c3 a9 together are é in UTF-8; 0a is the line feed. Convert the file to the ISO-8859-1 single-byte encoding, which represents é as one byte, e9:

$ iconv -f UTF-8 -t ISO-8859-1 /home/john/debian-handbook/shell-basics/enc/utf8.txt > enc/latin1.txt

The redirection writes the converted bytes to a new file; the terminal shows nothing. Verify both claims about the new file:

$ file /home/john/debian-handbook/shell-basics/enc/latin1.txt && od -An -tx1 /home/john/debian-handbook/shell-basics/enc/latin1.txt
/home/john/debian-handbook/shell-basics/enc/latin1.txt: ISO-8859 text
 63 61 66 e9 0a

file recognized the encoding from the byte pattern, and é is now the single byte e9. Convert back to see the text restored:

$ iconv -f ISO-8859-1 -t UTF-8 /home/john/debian-handbook/shell-basics/enc/latin1.txt
café

Not every byte sequence is valid input. A file beginning with ff fe is not UTF-8 at all (it looks like a UTF-16 byte-order mark), and iconv reports the position where it gave up:

$ printf "\377\376\n" > enc/invalid.txt
$ iconv -f UTF-8 -t ISO-8859-1 /home/john/debian-handbook/shell-basics/enc/invalid.txt
iconv: illegal input sequence at position 0

The message names the first offending byte; the exit status is 1. An "encoding error" in a tool almost always means the input is not in the encoding the tool was told to expect - check with file and od before choosing a conversion.

Line endings are the other classic. Unix text ends each line with a single line feed (0a); Windows text ends with carriage return plus line feed (0d 0a). A file saved by a Windows editor therefore looks normal but behaves oddly: configuration parsers report mystery characters, and shell scripts fail with "bad interpreter" or \r errors. Create one and ask file:

$ printf "one\r\ntwo\r\n" > enc/crlf.txt
$ file /home/john/debian-handbook/shell-basics/enc/crlf.txt
/home/john/debian-handbook/shell-basics/enc/crlf.txt: ASCII text, with CRLF line terminators

The repair is to delete the carriage returns at line ends. sed -i edits the file in place, and the substitution s/\r$// removes a \r at the end of each line:

$ sed -i 's/\r$//' enc/crlf.txt
$ file /home/john/debian-handbook/shell-basics/enc/crlf.txt
/home/john/debian-handbook/shell-basics/enc/crlf.txt: ASCII text

The dos2unix package exists for exactly this round trip, and announces what it did on standard error:

$ printf "alpha\r\nbeta\r\n" > enc/dos.txt
$ dos2unix /home/john/debian-handbook/shell-basics/enc/dos.txt; file /home/john/debian-handbook/shell-basics/enc/dos.txt
dos2unix: converting file /home/john/debian-handbook/shell-basics/enc/dos.txt to Unix format...
/home/john/debian-handbook/shell-basics/enc/dos.txt: ASCII text
$ unix2dos /home/john/debian-handbook/shell-basics/enc/dos.txt; file /home/john/debian-handbook/shell-basics/enc/dos.txt
unix2dos: converting file /home/john/debian-handbook/shell-basics/enc/dos.txt to DOS format...
/home/john/debian-handbook/shell-basics/enc/dos.txt: ASCII text, with CRLF line terminators

What "text" means in all of the above is set by the locale: the language and encoding conventions the system uses. The current character map is one query:

$ locale charmap
UTF-8

The locales installed on the example system are minimal:

$ locale -a
C
C.utf8
POSIX

C and POSIX are the plain-ASCII default; C.utf8 is its UTF-8 counterpart, and one of the two is what a server normally runs with unless it must sort or display text in a specific language. Debian's locales package provides /etc/locale.gen, where you enable the locales you need by uncommenting lines and running locale-gen as root; that installs them for the whole system. A program that prints é as é or fails on non-ASCII input is usually running under a locale whose character map does not match the data - check locale charmap before anything else.