Linux Command Line · Lesson 12 of 43
Viewing files with cat, less, head, tail and wc
Read text files from the command line with cat, less, head, tail, nl and wc, and identify unknown files with the file command.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
Most of your time on the command line is spent reading: configuration files, logs, scripts, data. You will learn which command to pick for each kind of reading, from "show me everything" to "just tell me how long it is".
cat: show the whole file
cat (short for concatenate) prints the contents of one or more files. It is perfect for short files:
cd ~/project
cat docs/notes.txtLinux is a family of operating systems.
The shell reads your commands.
A terminal is the window you type in.
Commands are small programs.
Practice makes them stick.Give it several files and it prints them one after another, which is where the name comes from. Add -n to number the output lines, or -A to show hidden characters such as tabs (^I) and line ends ($), which helps when a file behaves oddly:
cat -n docs/tasks.txt 1 Buy milk
2 Call the bank
3 Read chapter 3
4 Buy bread
5 Water the plantsless: scroll through long files
For a long file, cat floods the screen. less opens it in a viewer that does not change the file:
less logs/app.logInside less, press Space to go down a page, b to go back, /ERROR to search forward (then n for the next match), g and G to jump to the start or end, and q to quit. It is the same viewer that shows man pages, so you already know the keys. Because less needs a real terminal, we cannot show its output here.
head and tail: the start and the end
head shows the first ten lines by default, and -n changes that:
head -n 3 logs/app.log2026-01-15 10:00:01 INFO server started
2026-01-15 10:00:05 INFO user asha logged in
2026-01-15 10:01:12 WARN disk usage at 81%tail shows the last lines, which is usually where the newest log entries are:
tail -n 3 logs/app.log2026-01-15 10:02:44 ERROR database timeout
2026-01-15 10:03:00 INFO connection restored
2026-01-15 10:04:30 INFO user ben logged inWith a plus sign, tail starts at a line instead of counting from the end, so tail -n +2 skips a header line, which is useful for CSV files:
tail -n +2 data/scores.csvAsha,blue,82
Ben,red,67
Chitra,blue,91
Dev,green,74
Ela,red,88
Farid,green,67tail -f: watch a file grow
tail -f prints the last lines and then stays running, printing each new line as a program writes it. It is the standard way to watch a log live:
tail -f /var/log/syslogThat log file exists on Debian and Ubuntu; other systems use /var/log/messages or journalctl -f. Press Ctrl+C to stop. Use tail -F when the file might be rotated.
wc: count things
wc (word count) reports lines, words and bytes. Pick one number with an option:
wc docs/notes.txt
wc -l logs/app.log
wc -w docs/notes.txt 5 28 165 docs/notes.txt
8 logs/app.log
28 docs/notes.txtThe first command shows lines, words and bytes, in that order, followed by the file name. When wc reads from a pipe instead of a file, it prints just the numbers, which makes it good for use in scripts.
nl and tac
nl numbers lines more flexibly than cat -n, and tac (cat backwards) prints a file from the last line to the first:
tac docs/tasks.txtWater the plants
Buy bread
Read chapter 3
Call the bank
Buy milkfile: what kind of file is this?
A file's extension is only a hint. file looks at the contents and tells you what it really is:
file docs/notes.txt src/hello.shdocs/notes.txt: ASCII text
src/hello.sh: Bourne-Again shell script, ASCII text executableThe wording can differ slightly between versions of file. It is also the safe way to find out whether something is text before you try to cat it: printing a binary file can fill your screen with strange characters. If that happens, type reset and press Enter.
Common mistakes
Using cat on huge files. Use less, head or tail instead.
Forgetting that less is a program, not a command output. It waits for you. Press q to quit.
Reading counts wrong. wc -l counts newline characters, so a last line without a trailing newline is not counted.
Opening binary files with cat. Check with file first.
Exercise
Show the first two and the last two lines of logs/app.log. Count its lines with wc -l. Number the lines of docs/notes.txt with cat -n. Use tail -n +2 to print data/scores.csv without its header. Finally, run file on three different files from the project folder and note what it says about each.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.