How to Watch a File in Linux using tail

Published Apr 30, 2020  ∙  Updated May 2, 2022

We all know how we can view files using cat or less, but one command that I find very useful is the tail command.

Similar to the head command, it prints part of a file. But unlike head, it prints the last part of a file.

tail [flags] [file]

The -n Flag

By default, running tail on a file will print the last 10 lines of that file.

tail file.txt

Running tail -n k [file] option will allow us to print the last k lines of a file.

tail -n 5 file.txt

I don’t see myself using -n that often, though. I do see myself using -f quite often.

The -f Flag

Running -f by itself will print the last 10 lines of the file and then wait for new lines to be added to the file.

tail -f file.txt

It essentially waits and watches the file for something to be appended to it.

This is especially useful when you have a script or program running that outputs some results to a log file (usually a text file). You can just watch or “follow” (hence the -f) that log file in that terminal.

You can break out of this process by pressing Ctrl+C.