Viewing text with cat, less, head and tail

Beginner · 9 min

Read files with cat, page through them with less, sample the top with head and the end with tail -n, follow growing logs with tail -f, and spot BSD vs GNU gaps.

Before thisPipes and pipelines

CodeLucky · 9 min

Viewing text with cat, less, head and tail

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodeLuckyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • cat prints files in order and concatenates several into one stream. It is right for short files and for feeding pipelines, wrong for large ones.
  • less is a pager: space moves down, / searches, q quits. When its output is piped, it simply prints, like cat.
  • head -n N shows the first N lines and tail -n N the last N. tail -n +N starts at line N, which is the usual way to skip a header row.
  • tail -f keeps the file open and prints new lines as they are appended, which is how you watch a live log.
  • On macOS, head -n -2 fails and tac and cat -A do not exist. Use tail -r, cat -etv, or sed as the BSD-friendly route.

Looking at text from the shell

Most of what you meet in a terminal is text: logs, configs, CSVs, command output. Before you can search or transform it you need to look at it. Four commands cover nearly all of that, and which to choose comes down to how much text there is and where you want to look.

I used the CSV from this roadmap’s fixture (six lines, data/people.csv) and the 200-line access log.

cat: everything, in order

cat prints files to stdout. Its name is short for concatenate, because given several files it joins them:

$ cat data/people.csv
name,team,city,score
ada,infra,london,91
linus,kernel,portland,88
grace,compilers,new york,95
ken,tools,murray hill,79
margaret,flight,boston,97
$ printf 'A\n' > a; printf 'B' > b; cat a b a | od -c | head -1
0000000    A  \n   B   A  \n

The od -c output shows b had no trailing newline, so B and the next file’s A ran together on one line. cat does exactly what it is told and adds nothing. cat -n numbers lines. To see invisible characters, BSD cat takes -e (mark line ends with $), -t (tabs as ^I) and -v:

$ printf 'a\tb  \n' | cat -etv
a^Ib  $

That showed a tab and two trailing spaces, the kind of thing that breaks a script. GNU’s shortcut cat -A doesn’t exist on macOS (cat: illegal option -- A). Windows line endings show as ^M: printf 'a\r\nb\r\n' | cat -v printed a^M and b^M.

cat suits small files and feeding pipelines. A 2 GB log dumped to the screen is useless.

less: a pager for big files

less shows a screenful at a time. The keys to know: space (next page), b (back), /text (search forward, n for next match), g and G (top and bottom), q (quit). It doesn’t load the whole file first, so it opens a huge log instantly.

I couldn’t show an interactive session, since my shell had no terminal. What I could check is that less behaves like cat when its output isn’t a terminal: less data/people.csv | head -2 printed the first two lines, with no paging. My less is version 668. more is the older, weaker pager, and it also printed plainly in a pipe.

head and tail: the ends of a file

These show the first or last lines, 10 by default:

$ head -n 2 data/people.csv
name,team,city,score
ada,infra,london,91
$ tail -n 2 data/people.csv
ken,tools,murray hill,79
margaret,flight,boston,97
$ head -c 12 data/people.csv; echo '|'
name,team,ci|

-n counts lines and -c counts bytes. Given several files, head labels each one with a header like ==> data/people.csv <==.

Combine them for a range: head -n 4 file | tail -n 2 gave lines 3 and 4. sed -n '2,3p' does it in one command, which a later lesson covers.

tail has a form that is more useful than it looks: tail -n +N means start at line N:

$ tail -n +2 data/people.csv | head -2
ada,infra,london,91
linus,kernel,portland,88

That is the standard way to drop a header row. Sizing up a file is wc -l logs/access.log, which printed 200 logs/access.log; the last entry came from tail -n 1.

Following a live file: tail -f

tail -f prints the last lines and then stays open, printing new lines as another process appends them. That is how you watch a log during a deploy. I tested it by backgrounding tail -f live.log, appending two lines a second apart, and reading what it captured:

event 1
event 2

Stop it with Ctrl-C. A caveat I did not test: -f follows the open file, so when a log is rotated (renamed and recreated) it can keep watching the old one. tail -F follows the name and reopens. That behaviour is standard but I didn’t reproduce a rotation.

Where macOS differs

The BSD tools drop some GNU habits. All of these I ran:

$ head -n -2 data/people.csv
head: illegal line count -- -2
$ tac data/people.csv
bash: tac: command not found
$ tail -r data/people.csv | head -2
margaret,flight,boston,97
ken,tools,murray hill,79

On Linux, head -n -2 means “everything except the last two lines” and tac reverses a file. On macOS use tail -r to reverse. For “all but the last N”, I checked two routes that both ended on the right line: sed '$d' file | sed '$d', and head -n $(( $(wc -l < file) - 2 )) file. cat file | head and head file print the same, so skip the cat: it only adds a process.

About the video

CodeLucky’s “Linux File Viewing Commands: cat, less, tail, head & more for Beginners!” was published 6 April 2025 and runs 8 minutes 36 seconds. Its description lists the topics and its chapters give each command a slot: cat at 0:12, less at 1:10, more at 2:01, head at 2:49, tail at 3:42, combining commands at 4:45, use cases at 5:49 and a summary at 7:00. English per its title and description, but this channel publishes auto-dubbed tracks, so I’m judging English from the text and not from hearing it. I couldn’t read a transcript, so I haven’t checked it on tail -n +N, -F, or anything BSD-specific, and the video targets Linux. Learn Linux TV’s 18-minute head and tail video is the deeper pick. The same channel made the anatomy-of-a-command video earlier in this roadmap, so it appears twice in my picks.

What I did not run

Interactive less, tail -F through a real rotation, Linux GNU head, tac and cat -A, and bat or other modern viewers.

Next

Looking is slow once there is a lot to look at. Searching finds the lines that matter: searching with grep.

Check yourself

  1. 01How do you print a CSV file without its header row?

    tail -n +2 file.csv. The +2 means start output at line 2, so line 1 is skipped.

  2. 02You want to watch a log file as new lines arrive. Which command, and how do you stop it?

    tail -f file.log. Press Ctrl-C to stop it. tail -F also follows through log rotation, which -f does not.

  3. 03Why is cat bigfile | less a poor habit compared to less bigfile?

    It starts an extra process, and less loses the file name and some features. less already reads a file directly and pages it.

  4. 04Your head -n -2 file works on Linux but errors on your Mac. What can you use instead?

    BSD head does not accept a negative count. Print all but the last 2 lines with sed '$d' twice, or count lines with wc -l and use head -n with the arithmetic result.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark