Viewing Files with cat, less, head, and tail
Text files are everywhere on a Linux system: configuration, source code, logs, notes. Four commands cover almost every way of reading them from the terminal. cat prints a whole file, less lets you page through it, head shows the beginning, and tail shows the end. Each one suits a different situation, and knowing which to reach for saves a lot of scrolling.
Understanding the Concept
All four commands read a file and write text to standard output, which is normally your terminal. None of them modifies the file. The differences are in how much they show and how they let you interact:
| Command | Shows | Interactive |
|---|---|---|
cat | The entire file at once | No |
less | The file one screen at a time | Yes: scroll and search |
head | The first lines (10 by default) | No |
tail | The last lines (10 by default) | No (but can follow growth with -f) |
The examples below use two small files:
printf 'line one\nline two\nline three\n' > short.txt
seq 1 25 > nums.txt
The first creates a three-line file with printf. The second uses seq to write the numbers 1 to 25, one per line.
Printing a Whole File with cat
The name cat comes from "concatenate". With one file it prints the content:
cat short.txt
Output:
line one
line two
line three
The command is the right choice for short files, where you want everything at once. For a file with thousands of lines, the content would scroll past faster than you could read it, and less is a better tool.
Numbering Lines
The option -n puts a line number in front of every line:
cat -n short.txt
Output:
1 line one
2 line two
3 line three
Seeing Hidden Characters
Invisible characters can cause bugs, such as trailing spaces or tabs in a script. The option -A makes them visible. A tab appears as ^I, and the end of each line is marked with $:
printf 'a\tb \n' | cat -A
Output:
a^Ib $
The $ sits right after two spaces, which shows that the line ends with trailing spaces that you could not see before.
Squeezing Blank Lines
The option -s collapses runs of blank lines into a single blank line:
printf 'alpha\n\n\n\nbeta\n' > gaps.txt
cat -s gaps.txt | cat -A
Output:
alpha$
$
beta$
The three blank lines became one. The second cat -A is only there to show where lines end.
Joining Files
Given several names, cat prints them one after another, which is the source of its name. Combined with redirection, this joins files:
printf 'first\n' > p1
printf 'second\n' > p2
cat p1 p2 > joined.txt
cat joined.txt
Output:
first
second
A related command, tac (the word cat reversed), prints the lines in reverse order:
tac short.txt
Output:
line three
line two
line one
Reading Standard Input
When no file is given, cat reads from standard input and copies it to the output:
echo typed | cat
Output:
typed
Missing Files
cat nope.txt
echo "status=$?"
Output:
cat: nope.txt: No such file or directory
status=1
Reading Page by Page with less
less opens a file in a pager, a program that displays one screen at a time and waits for your keys. It does not load the whole file first, so it starts immediately even on very large files.
less nums.txt
Inside less, these keys are the most useful:
| Key | Action |
|---|---|
Space or f | Forward one screen |
b | Back one screen |
Down arrow or j | Forward one line |
Up arrow or k | Back one line |
g | Jump to the start of the file |
G | Jump to the end of the file |
/text | Search forward for text |
?text | Search backward for text |
n and N | Next and previous match |
q | Quit |
After typing /text and pressing Enter, less highlights the matches and jumps to the first one. Pressing n moves to the next.
Useful options include -N to show line numbers, -S to cut long lines instead of wrapping them, and -F to quit automatically when the whole file fits on one screen. Because of that last option, less -F behaves like cat for short input:
printf 'one\ntwo\nthree\n' | less -F -X
Output:
one
two
three
(The -X option stops less from clearing the screen on exit, so the text stays visible.) The manual page for less is itself shown with a pager, which is why the keys above work in man too.
Showing the Start of a File with head
head prints the first 10 lines by default:
head nums.txt
Output:
1
2
3
4
5
6
7
8
9
10
Use -n to choose the number of lines:
head -n 3 nums.txt
Output:
1
2
3
The short form head -3 also works in GNU head, but -n 3 is the portable choice. A negative count means "everything except the last N lines":
head -n -22 nums.txt
Output:
1
2
3
The file has 25 lines, so removing the last 22 leaves the first three.
The option -c counts bytes instead of lines:
head -c 10 nums.txt
Output:
1
2
3
4
5
The ten bytes are the five digits 1 to 5, each followed by a newline character, so the output looks like five lines.
Showing the End of a File with tail
tail prints the last 10 lines by default, and -n changes the count:
tail -n 3 nums.txt
Output:
23
24
25
A count with a plus sign changes the meaning: +N means "start at line N and print the rest".
tail -n +23 nums.txt
Output:
23
24
25
The same three lines appear, because line 23 is where the output begins.
Extracting a Range of Lines
Combining head and tail with a pipe prints any range. To see lines 10 to 12, take the first 12 lines and keep the last three of those:
head -n 12 nums.txt | tail -n 3
Output:
10
11
12
Several Files at Once
When you give head or tail more than one file, each section starts with a header naming the file:
head -n 2 short.txt nums.txt
Output:
==> short.txt <==
line one
line two
==> nums.txt <==
1
2
Following a Growing File
The option -f (follow) keeps tail running and prints new lines as they are added to the file. It is the standard way to watch a log:
tail -f app.log
If another program appends three events to app.log, the terminal shows each one as it arrives:
event 1
event 2
event 3
The command does not end by itself. Press Ctrl + C to stop it. The variant tail -F also keeps following when the log file is rotated, meaning replaced by a new file with the same name, which is common for server logs.
Practical Applications
Counting Before Reading
Before opening a file, check how big it is. The wc -l command counts lines:
wc -l nums.txt
Output:
25 nums.txt
A short file suits cat. A longer one calls for less, or head to peek at the beginning.
Inspecting the Start of Command Output
All four commands also read from a pipe. This shows only the first three entries of a long directory listing:
ls /usr/bin | head -n 3
The exact names depend on your system, but you receive only three lines instead of hundreds.
Checking a File Type by Its First Bytes
Binary files begin with a recognizable signature. Printing the first four bytes of a program in readable form, with head -c and od -c, shows the signature of the Linux executable format:
head -c 4 /bin/ls | od -c
Output:
0000000 177 E L F
0000004
The bytes are the value 177 in octal followed by the letters ELF, which stands for Executable and Linkable Format. This is why you should not use cat on binary files: the raw bytes can confuse your terminal.