Skip to lesson

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.

Beginner to intermediate·12 min·Working with files and folders·Updated 2026-11-24

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:

bash
cd ~/project
cat docs/notes.txt
result
Linux 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:

bash
cat -n docs/tasks.txt
result
     1	Buy milk
     2	Call the bank
     3	Read chapter 3
     4	Buy bread
     5	Water the plants

less: scroll through long files

For a long file, cat floods the screen. less opens it in a viewer that does not change the file:

text
less logs/app.log

Inside 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:

bash
head -n 3 logs/app.log
result
2026-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:

bash
tail -n 3 logs/app.log
result
2026-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 in

With 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:

bash
tail -n +2 data/scores.csv
result
Asha,blue,82
Ben,red,67
Chitra,blue,91
Dev,green,74
Ela,red,88
Farid,green,67

tail -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:

text
tail -f /var/log/syslog

That 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:

bash
wc docs/notes.txt
wc -l logs/app.log
wc -w docs/notes.txt
result
  5  28 165 docs/notes.txt
8 logs/app.log
28 docs/notes.txt

The 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:

bash
tac docs/tasks.txt
result
Water the plants
Buy bread
Read chapter 3
Call the bank
Buy milk

file: 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:

bash
file docs/notes.txt src/hello.sh
result
docs/notes.txt: ASCII text
src/hello.sh:   Bourne-Again shell script, ASCII text executable

The 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.

0 / 4 understood
01 Which command shows only the last 3 lines of a file?
02 What does tail -f app.log do?
03 How do you count the lines in a file?
04 Which key leaves less?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses