cat — print file contents
cat (concatenate) prints a file to the terminal.
$ cat notes.txt
Remember to water the plants.
It can print several files in sequence:
$ cat part1.txt part2.txt
Useful flags:
| Flag | Meaning |
|---|---|
-n |
number all output lines |
$ cat -n notes.txt
1 Remember to water the plants.
cat is fine for short files. For anything longer, use less.
Graphical alternative: any text editor or file viewer opens a file with a double-click.
less — page through files
less displays a file one screen at a time. It does not flood the terminal
the way cat does with long files.
$ less /var/log/syslog
(On Fedora the log lives in the journal instead — journalctl in
Chapter 14 reads it, and sudo dnf install rsyslog adds the file. On
Ubuntu the file is there from the start.)
While in less:
| Key | Action |
|---|---|
Space / Page Down |
next page |
b / Page Up |
previous page |
g |
go to the top |
G |
go to the bottom |
/word |
search forward |
q |
quit |
less also reads from a pipe, which is the most common way to use it.
(A pipe, |, sends one command’s output into the next command — it
gets a full treatment in Chapter 13.)
$ ps aux | less
head — show the start
head shows the first ten lines by default.
$ head /var/log/syslog
Use -n for a different count:
$ head -n 3 notes.txt
line1
line2
line3
head -c limits by bytes instead of lines:
$ head -c 100 file.bin > first100.bin
tail — show the end
tail shows the last ten lines by default.
$ tail /var/log/syslog
Use -n for a different count:
$ tail -n 5 notes.txt
The -f flag (follow) keeps the file open and prints new lines as they are
appended. This is invaluable for watching logs live:
$ tail -f /var/log/syslog
Press Ctrl+C to stop following.
Graphical alternative: GNOME Logs or your file viewer can show the same information, but
tail -fis often faster.
nano — the beginner-friendly editor
nano is a small text editor that runs in the terminal. It shows its key
bindings at the bottom of the screen, which makes it ideal for newcomers.
$ nano notes.txt
Key bindings (the ^ means the Ctrl key):
| Key | Action |
|---|---|
Ctrl+O |
write out (save) the file |
Ctrl+X |
exit nano |
Ctrl+G |
get help |
Ctrl+K |
cut the current line |
Ctrl+U |
paste the cut line |
Ctrl+W |
search within the file |
The basic workflow: type nano file, edit, press Ctrl+O then Enter to
save, then Ctrl+X to exit.
Default status: nano is preinstalled on Ubuntu desktops. On Fedora
and Arch it usually needs installing first: sudo dnf install nano /
sudo pacman -S nano. If nano is missing, vim-tiny or vi are present
on most systems as a fallback.
echo — print text
echo prints its arguments to the terminal.
$ echo hello
hello
echo is mostly used in scripts and with shell features, for example to
write to a file:
$ echo "Hello world" > greeting.txt
$ cat greeting.txt
Hello world
| Flag | Meaning |
|---|---|
-n |
no trailing newline |
$ echo -n "no newline here"
echo with -e interprets escape sequences like \t (tab):
$ echo -e "col1\tcol2"
col1 col2