cat, less, head, and tail

What this is. You constantly need to see what is in a file without opening an editor. Four commands cover it, and they differ mainly in how much they show you.

cat file dumps the whole thing at once.

less file opens a scrollable pager you leave by pressing q.

head file shows the first ten lines.

tail file shows the last ten.

Choosing between them

WHEN TO USE

cat  small files, or piping
less large files you must read
head is this the file I want
tail what happened most recently

WHY IT MATTERS

cat a 40000 line log
  -> the whole thing flies past

less the same log
  -> scroll, search, then quit
Compare → cat on a huge file is the classic beginner mistake. It is not dangerous, it is just useless, because everything scrolls past faster than you can read. less exists precisely for that case.

Counting is often what you actually wanted. wc counts, and its flags decide what.

wc -l file counts lines.

wc -w file counts words.

wc -c file counts characters.

wc -l is one of the most used commands in daily work, because how many is a question you ask constantly.

SANKOFA SANDBOX
learner:~$

The logs directory has two files worth poking at.

Try it → Run `cat logs/app.log`, then `head -n 3 logs/app.log`, then `tail -n 2 logs/app.log`, then `wc -l logs/app.log`. Four views of the same file, each answering a different question.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Large Files and Live Files →
← Back