You have found a configuration file or a log, but how do you see what it contains? A log is a file where a program writes down what it does, line after line. On Linux, four commands cover everyday file reading: cat to show, less to page through, head for the start, tail for the end. This guide shows you when and how to use each one.
What you will learn
- Show a whole file with
cat - Page through a long file with
less - Extract the first lines with
head - Extract the last lines with
tail - Follow a log file in real time with
tail -f
cat: show a whole file
cat (concatenate) prints the whole contents of a file in one go. For a short file, a few lines, it is the most direct command:
cat /etc/hostnamesrv-debianShow line numbers
The -n option numbers each line, which helps you find your way in a configuration file:
cat -n /etc/ssh/sshd_config 1 # This is the sshd server system-wide configuration file. 2 # 3 Include /etc/ssh/sshd_config.d/*.conf 4 #Port 22Concatenate several files
As its name says (concatenate), cat can print several files one after the other, in the order you give them:
cat header.csv data.csvproduct;quantity;pricepen;12;1.50notebook;4;3.20The output follows the order of the arguments: the lines of header.csv
appear first, those of data.csv next. Swapping the two names swaps the
display.
less: page through a long file
less opens the file in an interactive pager. You move page by page without the terminal scrolling all at once. The example uses the access log of an nginx web server, which quickly grows to thousands of lines:
less /var/log/nginx/access.logMoving around in less
A handful of keys is enough to move around, and they are the same as in
man, which uses less as its pager. The search keys
matter most on a long log:
| Key | Action |
|---|---|
Space or f | Next page |
b | Previous page |
g | Go to the start |
G | Go to the end |
/word | Search "word" downwards |
?word | Search "word" upwards |
n | Next match |
q | Quit |
more, the ancestor of less
more does a similar job, but it is more limited: it is designed to move forward. The b key (or Ctrl+B) goes back one screen when more reads a file, but not when it reads a pipe, that is, the output of another command that you send to it with |. less replaces it in almost every case, so remember mainly less.
head: the first lines
head prints the first 10 lines of a file by default:
head /etc/passwdTo show only the first 5:
head -n 5 /etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/syncTypical use case: quickly check the format of a CSV or configuration file before processing it. To see the header of a CSV and its first two data lines:
head -n 3 sales.csvproduct;quantity;pricepen;12;1.50notebook;4;3.20tail: the last lines
tail prints the last 10 lines by default:
tail /var/log/nginx/access.logFor the last 20, for example after an incident:
tail -n 20 /var/log/nginx/access.logFollow a file in real time with tail -f
The -f option (follow) keeps the terminal open and prints new lines as they arrive. It is the basic command to monitor a log:
tail -f /var/log/nginx/access.logEach time an event is written to the file, the line appears immediately. To quit, press Ctrl+C.
Which command should you choose?
The choice depends on two things: the length of the file and what you are looking for in it. The table sums up six common situations.
| Need | Command |
|---|---|
| See a small file (< 30 lines) | cat file |
| See a small file with line numbers | cat -n file |
| Page through a long file | less file |
| See the start of a file | head file or head -n 5 file |
| See the end of a file | tail file or tail -n 20 file |
| Monitor a log in real time | tail -f file |
Troubleshooting
These commands rarely fail, and when they do the message or the symptom points straight at the cause. A screen full of strange characters often surprises beginners: you have opened a binary file.
| Problem | Likely cause | Fix |
|---|---|---|
cat: the terminal scrolls too fast | File too long | Use less |
less: command not found | Package not installed (rare) | sudo apt install less (Debian, Ubuntu) or sudo dnf install less (RHEL, AlmaLinux) |
tail -f: nothing shows up | The file is not being written to right now | Check with ls -l that the file is growing |
Permission denied | Your account is not allowed to read this file | Find the owner with ls -l, then put sudo in front of the command if it is a system file that is normally read as administrator |
| Strange characters on screen | Binary file shown by mistake | Check the type with file file_name |
Key points
catfor short files,lessfor long files.headandtailextract the start or the end; control the number of lines with-n.tail -fprints the new lines of a log as they arrive.- When in doubt about the length of a file, start with
less: it does not flood the terminal.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Find files on Linux
A new full quiz with random questions
Work again only on the questions you missed