Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Read file contents on Linux

By updated

Improve this page

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:

Terminal window
cat /etc/hostname
Output
srv-debian

Show line numbers

The -n option numbers each line, which helps you find your way in a configuration file:

Terminal window
cat -n /etc/ssh/sshd_config
Output (excerpt)
1 # This is the sshd server system-wide configuration file.
2 #
3 Include /etc/ssh/sshd_config.d/*.conf
4 #Port 22

Concatenate several files

As its name says (concatenate), cat can print several files one after the other, in the order you give them:

Terminal window
cat header.csv data.csv
Output (example)
product;quantity;price
pen;12;1.50
notebook;4;3.20

The 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:

Terminal window
less /var/log/nginx/access.log

Moving 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:

KeyAction
Space or fNext page
bPrevious page
gGo to the start
GGo to the end
/wordSearch "word" downwards
?wordSearch "word" upwards
nNext match
qQuit

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:

Terminal window
head /etc/passwd

To show only the first 5:

Terminal window
head -n 5 /etc/passwd
Output
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

Typical 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:

Terminal window
head -n 3 sales.csv
Output (example)
product;quantity;price
pen;12;1.50
notebook;4;3.20

tail: the last lines

tail prints the last 10 lines by default:

Terminal window
tail /var/log/nginx/access.log

For the last 20, for example after an incident:

Terminal window
tail -n 20 /var/log/nginx/access.log

Follow 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:

Terminal window
tail -f /var/log/nginx/access.log

Each 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.

NeedCommand
See a small file (< 30 lines)cat file
See a small file with line numberscat -n file
Page through a long fileless file
See the start of a filehead file or head -n 5 file
See the end of a filetail file or tail -n 20 file
Monitor a log in real timetail -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.

ProblemLikely causeFix
cat: the terminal scrolls too fastFile too longUse less
less: command not foundPackage not installed (rare)sudo apt install less (Debian, Ubuntu) or sudo dnf install less (RHEL, AlmaLinux)
tail -f: nothing shows upThe file is not being written to right nowCheck with ls -l that the file is growing
Permission deniedYour account is not allowed to read this fileFind 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 screenBinary file shown by mistakeCheck the type with file file_name

Key points

  • cat for short files, less for long files.
  • head and tail extract the start or the end; control the number of lines with -n.
  • tail -f prints 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

6 questions
6 min
70% to pass

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