ShareYourGroupShareYourGroupCommands

Linux and Mac commands

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

Linux or Mac terminal
asha@pc:~/demo$ cat notes.txt
Buy milk
Call the plumber
Pay the electricity bill

Good for short files. -n numbers the lines.

Linux or Mac terminal
asha@pc:~/demo$ cat -n marks.csv
     1	name,marks
     2	Asha,78
     3	Ravi,45
     4	Meena,92
     5	Kabir,33
     6	Tara,67

The name comes from "concatenate": given several files, cat prints them one after another, which joins them.

Linux or Mac terminal
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: 18700

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

Linux or Mac terminal not run here
less log.txt
KeyAction
Spacenext screen
bprevious screen
Down arrow or Enternext line
ggo to the start
Ggo to the end
/wordsearch forward for a word
nnext match
qquit

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

Linux or Mac terminal
asha@pc:~/demo$ head -n 3 log.txt
09:00 INFO  Program started
09:02 INFO  Opened report.txt
09:05 ERROR Printer not found

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

Linux or Mac terminal
asha@pc:~/demo$ head -n 1 marks.csv
name,marks

tail: the last lines

Linux or Mac terminal
asha@pc:~/demo$ tail -n 3 log.txt
09:10 WARN  Low disk space
09:15 INFO  Saved report.txt
09:16 INFO  Program closed

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

Linux or Mac terminal
asha@pc:~/demo$ tail -n +2 marks.csv
Asha,78
Ravi,45
Meena,92
Kabir,33
Tara,67

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

Linux or Mac terminal not run here
tail -f /var/log/syslog

A slice from the middle

Combine the two with a pipe. This shows lines 4 to 6:

Linux or Mac terminal
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 space

Counting

wc counts lines, words and bytes. -l gives lines only.

Linux or Mac terminal
asha@pc:~/demo$ wc log.txt
  8  34 222 log.txt
asha@pc:~/demo$ wc -l log.txt
8 log.txt

Which to use

FileUse
shortcat
longless
just the starthead
just the end, or a growing logtail, 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:

Linux or Mac terminal not run here
cat > shopping.txt

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