Viewing Files: cat, less, head & tail

Linux · Lesson 7 of 32 7 min read Updated

Most things you look at on a Linux system are plain text files: configuration in /etc, logs in /var/log, CSV exports, scripts and notes. You do not need an editor to read them. Four commands cover almost every case: cat for short files, less for long ones, and head and tail for the beginning and end.

In this lesson you will use them on Priya's files and on real system files, and you will learn tail -f, the command every server administrator keeps open while debugging.

cat: print a whole file

cat prints a file's contents straight to the terminal. It is perfect for short files:

Terminal
cat notes/todo.txt
cat -n notes/todo.txt
Output
Buy charger
Submit assignment
Call Kabir
     1  Buy charger
     2  Submit assignment
     3  Call Kabir

-n numbers every line, which helps when an error message says "line 42". The name cat comes from concatenate, meaning "join together": give it several files and it prints them one after another.

Terminal
cat notes/todo.txt notes/more.txt
cat notes/todo.txt notes/more.txt > notes/all-tasks.txt
Output
Buy charger
Submit assignment
Call Kabir
Pay electricity bill

The second line joins the two files into a new file instead of the screen; the > sign is redirection, which you will learn properly in Pipes & Redirection.

less: page through long files

A log file can have thousands of lines; cat would scroll them past in a blur. less is a pager: it shows one screen at a time and lets you move around and search. It does not load the whole file first, so it opens even very large files instantly.

Terminal
less /var/log/syslog
less +G /var/log/syslog

The second form opens the file at the end (G means "go to the last line"), which is usually where the newest log lines are. Inside less, use these keys:

KeyAction
Space / bNext page / previous page
Up / DownOne line up / down
g / GGo to the start / end of the file
/wordSearch forward for "word"; n next match, N previous
?wordSearch backward
FFollow new lines as they are added (Ctrl+C to stop following)
qQuit

man pages open in less too, so these keys work there as well. You may also meet more, an older and simpler pager that only scrolls forward; less replaced it ("less is more").

head and tail: the start and the end

head prints the first lines of a file, 10 by default. tail prints the last lines. The -n option chooses how many:

Terminal
head -n 3 orders.csv
tail -n 2 orders.csv
Output
order_id,customer,city,item,amount
1001,Aarav,Pune,Notebook,120
1002,Priya,Jaipur,Pen Set,250
1007,Farhan,Pune,Pen Set,250
1008,Isha,Kochi,Notebook,120

head is a quick way to see the column names of a CSV file without opening the whole thing. Two less obvious forms are very handy:

Terminal
tail -n +2 orders.csv | head -n 2
head -c 20 orders.csv
Output
1001,Aarav,Pune,Notebook,120
1002,Priya,Jaipur,Pen Set,250
order_id,customer,ci

tail -n +2 means "start from line 2", so it skips the header row. The | sends that output into head, which keeps only two lines. head -c 20 counts bytes instead of lines.

tail -f: watch a log live

When you are fixing a web server, you want to see new log lines the moment they are written. tail -f (follow) prints the last lines and then keeps waiting, printing each new line as it arrives:

Terminal
sudo tail -f /var/log/nginx/access.log
Output
203.0.113.7 - - [30/Oct/2026:10:01:12 +0530] "GET / HTTP/1.1" 200 5120 "-" "Mozilla/5.0"
198.51.100.23 - - [30/Oct/2026:10:01:15 +0530] "GET /menu.html HTTP/1.1" 200 3301 "-" "Mozilla/5.0"
192.0.2.55 - - [30/Oct/2026:10:03:02 +0530] "GET /wp-login.php HTTP/1.1" 404 162 "-" "curl/8.5.0"

Open the website in a browser and watch a new line appear for every request. Press Ctrl+C to stop. Logs are often rotated: the old file is renamed (for example to access.log.1) and a new empty one is created. Plain -f keeps watching the old renamed file; tail -F (capital F) notices the change and reopens the file by name, which is safer for long sessions.

A few more readers

  • tac prints a file with the lines in reverse order (it is cat backwards), handy for logs where you want the newest line first.
  • nl numbers lines like cat -n but skips blank lines by default.
  • wc -l file counts the lines, which tells you how long a file is before you open it.
  • zcat and zless read gzip-compressed files such as syslog.2.gz without unpacking them first.

Common mistakes

  • Using cat on huge files. Thousands of lines fly past and fill your scrollback. Use less, head or tail.
  • Getting "Permission denied" on logs. Many files in /var/log are readable only by root or the adm group. Use sudo less, or ask for your user to be added to adm.
  • Forgetting -n. head 5 file treats 5 as a file name. Write head -n 5 file.
  • Leaving tail -f running and wondering why the prompt is gone. It is still following; press Ctrl+C.

Key points

  • cat prints whole files and joins several files; cat -n numbers lines.
  • less pages through long files: / searches, G jumps to the end, q quits.
  • head -n N shows the first N lines and tail -n N the last N; tail -n +2 skips the first line.
  • tail -f (or tail -F for rotated logs) follows a file live until you press Ctrl+C.
  • Never cat binary files; check with file first and use reset if the terminal breaks.

Exercise

Using /etc/passwd (the list of user accounts, readable by everyone): show only the first line, show the last three lines with line numbers of their own, and open the file in less, search for your own user name and quit. Then explain what tail -n +5 /etc/passwd | head -n 1 prints.

Show solution
Terminal
head -n 1 /etc/passwd
tail -n 3 /etc/passwd | cat -n
less /etc/passwd
Output
root:x:0:0:root:/root:/bin/bash
     1  sshd:x:106:65534::/run/sshd:/usr/sbin/nologin
     2  priya:x:1000:1000:Priya Sharma:/home/priya:/bin/bash
     3  kabir:x:1001:1001:Kabir Singh:/home/kabir:/bin/bash

Inside less, type /priya, press Enter to jump to the match, then q. The command tail -n +5 /etc/passwd | head -n 1 starts printing at line 5 and keeps only the first of those lines, so it prints exactly the fifth line of the file.

Quick Quiz

1. Which command is best for reading a 50,000-line log file?

2. What does tail -n +2 orders.csv do?

3. You are following a log with tail -f. How do you stop?

4. Your terminal shows strange symbols after you ran cat on a picture. What fixes it?

Learn Linux with a mentor

Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call