cat, less, head and tail: read text files
Four small commands for looking inside text files without opening an editor. Between them they handle everything from a three-line note to a log of a million lines.
cat: print the whole file
asha@pc:~/demo$ cat notes.txt
Buy milk
Call the plumber
Pay the electricity billGood for short files. -n numbers the lines.
asha@pc:~/demo$ cat -n marks.csv
1 name,marks
2 Asha,78
3 Ravi,45
4 Meena,92
5 Kabir,33
6 Tara,67The name comes from "concatenate": given several files, cat prints them one after another, which joins them.
asha@pc:~/demo$ cat notes.txt report.txt
Buy milk
Call the plumber
Pay the electricity bill
Sales report for October
North region: 8750
South region: 5750
East region: 4200
Total: 18700Sent into a new file with >, that makes a combined file: cat part1.txt part2.txt > whole.txt.
Danger. cat a.txt b.txt > a.txt does not append b to a. The shell empties a.txt before cat reads it, and the contents are lost. Write to a different file name.
less: one screen at a time
For anything longer than a screen, use less. It opens the file in a viewer and waits for keys. Shown here without being run:
less log.txt| Key | Action |
|---|---|
| Space | next screen |
| b | previous screen |
| Down arrow or Enter | next line |
| g | go to the start |
| G | go to the end |
| /word | search forward for a word |
| n | next match |
| q | quit |
less reads only as much of the file as it shows, so it opens huge files instantly. It never changes the file. If you find yourself stuck in a screen full of text with a colon at the bottom, you are in less: press q.
head: the first lines
asha@pc:~/demo$ head -n 3 log.txt
09:00 INFO Program started
09:02 INFO Opened report.txt
09:05 ERROR Printer not foundWithout -n, head shows ten lines. It is the quick way to see what kind of file something is, or to read the heading row of a CSV.
asha@pc:~/demo$ head -n 1 marks.csv
name,markstail: the last lines
asha@pc:~/demo$ tail -n 3 log.txt
09:10 WARN Low disk space
09:15 INFO Saved report.txt
09:16 INFO Program closedLogs grow at the bottom, so the end is where the news is.
tail -n +2 means "from line 2 onwards", which skips a heading row.
asha@pc:~/demo$ tail -n +2 marks.csv
Asha,78
Ravi,45
Meena,92
Kabir,33
Tara,67Following a file as it grows
tail -f keeps the file open and prints new lines as they are written. It is how people watch a log while a program runs. Press Ctrl + C to stop.
tail -f /var/log/syslogA slice from the middle
Combine the two with a pipe. This shows lines 4 to 6:
asha@pc:~/demo$ head -n 6 log.txt | tail -n 3
09:06 INFO Retrying
09:07 ERROR Printer not found
09:10 WARN Low disk spaceCounting
wc counts lines, words and bytes. -l gives lines only.
asha@pc:~/demo$ wc log.txt
8 34 222 log.txt
asha@pc:~/demo$ wc -l log.txt
8 log.txtWhich to use
| File | Use |
|---|---|
| short | cat |
| long | less |
| just the start | head |
| just the end, or a growing log | tail, tail -f |
Not for every file
These are for text. A picture, a PDF or a program printed with cat fills the screen with symbols and can leave the terminal behaving oddly. If that happens, type reset and press Enter, even if you cannot see what you type. To find out what a file is before opening it, use file name.
Making a small file
cat with > and no input file takes what you type until you press Ctrl + D on an empty line:
cat > shopping.txtFor anything more than a few lines, an editor is easier. nano filename opens a simple one, with its commands listed at the bottom of the screen: Ctrl + O saves and Ctrl + X leaves.
The Windows equivalents are type and more.
Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.
This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.