Log inSign up

Reading files and following logs from the terminal

For a short file, cat file prints everything at once. For anything longer than a screen, less file lets you scroll, search with /word and quit with q. To see the end of a log use tail -n 50 file, and to watch new lines arrive in real time run sudo tail -F /var/log/nginx/error.log and stop it with Ctrl+C.

Topic: Linux command line · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Which tool should I pick for which file?

Match the tool to the size and the goal: cat for a few dozen lines, less for reading or searching anything bigger, head and tail for the start or end of a file, and tail with -f or -F for a file that is still growing. All of them come with every Linux image, since cat, head and tail belong to coreutils and less is installed by default.

cat /etc/hostname
less /etc/ssh/sshd_config
head -n 20 /etc/passwd
tail -n 100 /var/log/dpkg.log

Do not cat a multi gigabyte file into an SSH session: the terminal tries to draw every line and becomes unresponsive for a long time. If that happens, press Ctrl+C and switch to less or tail. A binary file printed with cat fills the screen with garbage and can confuse the terminal; run file name first when you are unsure, and reset repairs a scrambled terminal.

What can cat do besides printing a file?

Its name comes from concatenate: given several files, cat prints them one after the other, which also makes it a quick way to join parts into one file.

cat header.html body.html footer.html > page.html
cat -n /etc/fstab
cat -A script.sh | head
  • -n numbers every line, -b only non-empty lines.
  • -A reveals invisible characters: $ marks each line end, ^I a tab and ^M a Windows carriage return, the usual reason a shell script fails with /bin/bash^M: bad interpreter.
  • -s squeezes runs of empty lines into one.

The reverse tool, tac, prints a file from the last line to the first. To write a short file without an editor, a here document works well: the heredoc guide shows cat > file <<'EOF' in detail.

How do I move around and search in less?

less shows one screen at a time and loads only what it displays, so it opens even huge files instantly. Use these keys while it is open:

  • Space or f forward a page, b back a page, arrow keys one line.
  • g jumps to the first line, G to the last.
  • /text searches forward, ?text backward, n and N repeat the search.
  • &text filters the view to matching lines; & with an empty pattern shows all lines again.
  • F follows the file like tail -f; Ctrl+C stops following, q quits.
  • h opens the built-in help.
less -N /etc/nginx/nginx.conf
less -S /var/log/nginx/access.log
less +G /var/log/syslog

-N adds line numbers, -S chops long lines instead of wrapping them (scroll sideways with the arrow keys), and +G opens the file at its end. Searches are case sensitive unless you start less with -i. On Rocky and Alma the main system log is /var/log/messages, and on Debian 12 and 13 there is no syslog file by default because logs go to the systemd journal.

How do head and tail select lines?

head prints the beginning of a file and tail the end, ten lines each unless you give -n. A plus sign changes tail to mean "from this line to the end".

head -n 5 access.log
tail -n 30 access.log
tail -n +200 access.log | head -n 20
head -c 1K data.bin | xxd | head

The third line prints lines 200 to 219, a simple way to cut a slice out of a file. With -c both tools count bytes rather than lines. When given several files they print a small ==> name <== header before each part, which you can switch off with -q.

How do I follow a log file live?

Run tail -f on the file. It prints the last lines and then waits, showing each new line as the program writes it, until you press Ctrl+C.

sudo tail -f /var/log/nginx/access.log
sudo tail -F /var/log/nginx/error.log
sudo tail -F /var/log/nginx/access.log /var/log/nginx/error.log

Prefer -F for logs that get rotated. Plain -f keeps reading the old file after logrotate renames it and then sees nothing new, while -F notices the change and reopens the file under its name. Following two files at once shows lines from both with headers between them. To watch only lines that matter, pipe into grep with line buffering so matches appear at once:

sudo tail -F /var/log/auth.log | grep --line-buffered "Failed password"

On Rocky and Alma the SSH log is /var/log/secure. Services that log only to the journal are followed with journalctl -fu name, explained in the journalctl guide.

How do I read rotated and compressed logs?

Use the z versions of the tools, which decompress on the fly without creating files on disk. Rotated logs usually look like access.log.1 (plain) and access.log.2.gz (compressed).

ls /var/log/nginx/
zcat /var/log/nginx/access.log.2.gz | tail -n 20
zless /var/log/nginx/access.log.3.gz
zgrep " 500 " /var/log/nginx/access.log.*.gz

How many old files exist and how often they are rotated is decided by logrotate, covered in the logrotate guide. Reading these files usually needs sudo, or membership in the adm group on Ubuntu and Debian, which can read most files under /var/log: sudo usermod -aG adm deploy, then log in again.

How can I read a file that needs root rights?

Put sudo in front of the reader, not in front of a redirection. sudo less /etc/shadow works, while sudo cat file > copy writes the copy as your user and may fail.

sudo less /var/log/auth.log
sudo cat /etc/sudoers.d/90-deploy
sudo tail -n 20 /var/log/secure

less has a feature worth knowing on servers: pressing v opens the current file in your editor. Under sudo that becomes a root editor, so set LESSSECURE=1 or use sudoedit for edits instead. Reading is safe; it never changes the file.

Why does my terminal hang or show nothing?

  • cat with no file name waits for keyboard input. Press Ctrl+D or Ctrl+C.
  • tail -f shows no new lines: the log was rotated; use -F, or the service writes to the journal instead of a file.
  • Garbage on screen: you printed a binary file. Type reset and press Enter, even if you cannot see the letters.
  • Permission denied: the log belongs to root or adm; use sudo or join the adm group.
  • less shows ESC sequences: the file contains colour codes; open it with less -R.

Questions about Read files with cat, less and tail

What is the difference between tail -f and tail -F?

-f follows the open file, even after it is renamed. -F follows the name and reopens the file when logrotate replaces it, which is what you want for logs.

How do I show line numbers?

Use cat -n file, or less -N file for long files. Inside less you can also type -N and press Enter to toggle them.

Can less open compressed files?

Use zless file.gz, which works on all four distributions. On Ubuntu and Debian plain less can also read many compressed files through lesspipe.

Is more still useful?

more is the older pager and still installed, but less can do everything more does, plus scrolling backwards, searching and following.

How do I stop following a log?

Press Ctrl+C in tail. Inside less after pressing F, Ctrl+C stops following and leaves you in less; press q to exit.