Module 2 · Files and directories
Reading files: cat, more and less
cat for short files and for joining files together, more and less for everything longer, and the handful of keys that make less comfortable.
What you will learn
- Print short files with `cat`, numbered or showing hidden characters.
- Join several files into one with `cat` and `>`.
- Page through long files with `more` and `less`, and search inside `less`.
Reading a file is the most frequent thing you will do in a terminal: configuration, logs, notes, scripts. Three tools cover it. cat dumps the whole file at once, which is perfect for anything that fits on a screen. more and less are pagers: they show one screenful and wait for you, which is what you want for anything longer.
cat: print and concatenate
The name comes from *concatenate*. Given one file, cat prints it. Given several, it prints them one after another as a single stream, with nothing in between. Add > and a file name and that stream is saved instead of shown: cat part1.txt part2.txt > whole.txt joins two files into a third. Redirection gets its own lesson (22); for now, remember that > creates or replaces the file on its right.
~% cd /root/lab/l16
l16% cat part1.txt
Roses are red,
l16% cat part1.txt part2.txt
Roses are red,
violets are blue.
l16% cat part1.txt part2.txt > whole.txt
l16% cat -n whole.txt
1 Roses are red,
2 violets are blue.
l16% cat -A spaces.txt
1 name=ana $
city=^Imadrid$
Three options are worth knowing. -n numbers every line and -b numbers only the non-empty ones, handy when someone says "look at line 12". -A makes invisible characters visible: every line end becomes $ and every tab ^I, so trailing spaces and tabs that break configuration files suddenly show up. (This BusyBox build also prints a stray 1 before the first line with -A; ignore it.) BusyBox cat has no -s; you will meet other ways to squeeze blank lines later.
Pagers: more and less
cat on a 2 000-line log scrolls everything past and leaves you staring at the end. more FILE stops after each screen: Space for the next page, Enter for one more line, q to quit. It only goes forward. less is the improved version — the joke is that *less is more* — and lets you move in both directions, jump and search. It also reads from a pipe, so ls -l /bin | less turns any long output into something you can browse.
| Key in less | Action |
|---|---|
Space / b | Next page / previous page |
| Arrows, Enter | One line at a time |
g / G | Jump to the start / the end |
/word then n / N | Search forward; next / previous match |
q | Quit |
less -N shows line numbers and less -I makes searches ignore case. Unlike an editor, a pager can never change the file, so it is the safe way to look inside anything important.
Commands in this lesson
| Command | What it does |
|---|---|
cat FILE | Print a whole file. |
cat A B > C | Join A and B into a new file C. |
cat -n FILE | Print with line numbers (`-b`: non-empty lines only). |
cat -A FILE | Show line ends as `
|
more FILE | Page forward through a file. |
less FILE | Page in both directions and search with `/`. |
CMD | less | Browse a long output. |
Quiz
What does `cat a.txt b.txt` print?
- Only `a.txt`; the second name is ignored
- The contents of `a.txt` followed by those of `b.txt`
- The differences between the two files
In `less`, how do you search for the word `error`?
- Type `/error` and press Enter
- Press Ctrl+F and type `error`
- Quit and use `cat`
What is the main advantage of `less` over `more`?
- It can edit the file
- It can move backwards as well as forwards
- It prints the file faster
A config line looks right but fails. Which command reveals trailing spaces and tabs?
- `cat -n config`
- `cat -A config`
- `more config`
`cat part1.txt part2.txt > whole.txt`, and `whole.txt` already existed. What happens to its old contents?
- They are kept and the new text is added at the end
- They are replaced
- `cat` refuses to write to an existing file
Practice
Somewhere in the long file /root/lab/l16/secret.txt there is a line saying `the code word is: ...`. Read the file, then create an empty file named after the code word inside /root/lab/l16/answer.
Join /root/lab/l16/part1.txt and /root/lab/l16/part2.txt, in that order, into a new file /root/lab/l16/whole.txt.
Open /root/lab/l16/secret.txt in the pager that lets you scroll backwards and search, with line numbers shown. (Press `q` to leave it.)
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.