Linuxlab

Reading files in Linux: cat, head, tail

Updated July 3, 2026

cat prints a whole file, head prints the first lines, and tail prints the last.

Common cases

cat file.txt        # the whole file to the terminal
cat -n file.txt     # with line numbers
head -n 20 file     # the first 20 lines
tail -n 20 file     # the last 20 lines
tail -f app.log     # follow new lines in real time

How it works: cat versus a pager

cat dumps the whole file to the terminal at once, so for large files people use the pager less, which scrolls a page at a time. tail -f does not exit: it holds the file open and prints new lines as they arrive. This is the main trick for watching live logs. head and tail read only the edge of the file you ask for, so they stay fast even on gigabyte logs.

Used in

You need this in the lesson Reading text. The theory is the chapter Reading and editing text.

See also

The less pager, The file command, Redirection > and >>.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.