Viewing Files: cat, less, head & tail
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:
cat notes/todo.txt
cat -n notes/todo.txtBuy 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.
cat notes/todo.txt notes/more.txt
cat notes/todo.txt notes/more.txt > notes/all-tasks.txtBuy 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.
less /var/log/syslog
less +G /var/log/syslogThe 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:
| Key | Action |
|---|---|
Space / b | Next page / previous page |
Up / Down | One line up / down |
g / G | Go to the start / end of the file |
/word | Search forward for "word"; n next match, N previous |
?word | Search backward |
F | Follow new lines as they are added (Ctrl+C to stop following) |
q | Quit |
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:
head -n 3 orders.csv
tail -n 2 orders.csvorder_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:
tail -n +2 orders.csv | head -n 2
head -c 20 orders.csv1001,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:
sudo tail -f /var/log/nginx/access.log203.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
tacprints a file with the lines in reverse order (it iscatbackwards), handy for logs where you want the newest line first.nlnumbers lines likecat -nbut skips blank lines by default.wc -l filecounts the lines, which tells you how long a file is before you open it.zcatandzlessread gzip-compressed files such assyslog.2.gzwithout unpacking them first.
Common mistakes
- Using
caton huge files. Thousands of lines fly past and fill your scrollback. Useless,headortail. - Getting "Permission denied" on logs. Many files in
/var/logare readable only by root or theadmgroup. Usesudo less, or ask for your user to be added toadm. - Forgetting
-n.head 5 filetreats5as a file name. Writehead -n 5 file. - Leaving
tail -frunning and wondering why the prompt is gone. It is still following; pressCtrl+C.
Key points
catprints whole files and joins several files;cat -nnumbers lines.lesspages through long files:/searches,Gjumps to the end,qquits.head -n Nshows the first N lines andtail -n Nthe last N;tail -n +2skips the first line.tail -f(ortail -Ffor rotated logs) follows a file live until you pressCtrl+C.- Never
catbinary files; check withfilefirst and useresetif 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
head -n 1 /etc/passwd
tail -n 3 /etc/passwd | cat -n
less /etc/passwdroot: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/bashInside 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?
less shows one screen at a time, searches and opens large files instantly.
2. What does tail -n +2 orders.csv do?
A + before the number means "start at this line".
3. You are following a log with tail -f. How do you stop?
tail -f runs until interrupted, and Ctrl+C interrupts it.
4. Your terminal shows strange symbols after you ran cat on a picture. What fixes it?
reset reinitialises the terminal after binary output has confused 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
