← All lessons
Command line basics · Lesson 4 of 7

Reading files

cat prints a whole file. It's quickest for short files; for a long one the start scrolls away before you can read it.

less opens a file one screen at a time. Use the arrow keys or Space to move, / and a word to search, n for the next match, and q to quit. Many commands, like man, show their pages in less, so the same keys work there.

head shows the first 10 lines and tail the last 10. -n picks how many: head -n 3 list.txt. tail -f (follow) keeps printing new lines as they're added, which is how you watch a log file while something runs. Press Ctrl+C to stop.

$ cat ducks.txt
mallard
teal
pintail
eider
$ head -n 2 ducks.txt
mallard
teal
$ wc -l ducks.txt
4 ducks.txt

wc (word count) counts lines, words and bytes. wc -l counts just lines, which is handy for "how many entries are in this list?"

Text files are the heart of Linux: settings, logs and scripts are all plain text, so these few commands let you look at almost anything on the system.

Practice

Print the last 10 lines of app.log.

More info

Quick reference

cat filePrint the whole file.
less fileRead page by page. `/` search, `n` next, `q` quit.
head -n N, tail -n NFirst or last N lines (10 by default).
tail -f fileKeep showing new lines. Ctrl+C stops.
wc -l fileCount lines.

The other Linux courses