On this page
- Which tool should I pick for which file?
- What can cat do besides printing a file?
- How do I move around and search in less?
- How do head and tail select lines?
- How do I follow a log file live?
- How do I read rotated and compressed logs?
- How can I read a file that needs root rights?
- Why does my terminal hang or show nothing?
- Questions
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.logDo 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-nnumbers every line,-bonly non-empty lines.-Areveals invisible characters:$marks each line end,^Ia tab and^Ma Windows carriage return, the usual reason a shell script fails with/bin/bash^M: bad interpreter.-ssqueezes 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
fforward a page,bback a page, arrow keys one line. gjumps to the first line,Gto the last./textsearches forward,?textbackward,nandNrepeat the search.&textfilters the view to matching lines;&with an empty pattern shows all lines again.Ffollows the file like tail -f; Ctrl+C stops following,qquits.hopens 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 | headThe 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.logPrefer -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.*.gzHow 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/secureless 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
resetand 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.
More in Linux command line
- Copy files and directories with cp
- Move and rename with mv
- Create directories and empty files
- List files with ls, find your place with pwd
- Find files with find and locate
- Search text with grep
- Edit and report text with sed and awk
- echo, tee and wc
- Edit files with nano
- Edit files with Vim
- Read manual pages
- Users and groups
- Ownership with chown and chgrp
- Symbolic and hard links
- Pipes, redirection and xargs