Technical skill

Linux command: tail

Subject: .../LINUX

Many programs keep a log file, a running record of events such as starts, warnings, and errors. When a problem just happened, the newest lines are often the first place to look. tail prints the end of a file or a stream, so it can show recent records without making you read the entire file. A stream is data arriving from another command or program. This makes tail useful both for checking a saved file and for watching new output.

With no option, tail outputs the last ten lines; -n 50 selects fifty lines, while -c 512 selects the last 512 bytes. A starting position can also be given with a plus sign: tail -n +2 file.txt prints from line two through the end. With no file, or with - as the filename, tail reads standard input and can finish a pipeline. For standard input from a pipe or FIFO, GNU tail ignores -f when no file operand is given or the operand is -; there is no path to reopen.

For example, tail -n 30 application.log gives a quick view of recent records without printing the entire file. When multiple files are supplied, tail usually prints a header before each file's output. That is useful for a person comparing logs, but scripts should account for headers. A text line is not necessarily a complete application event when records span multiple lines.

To watch new output from a running program, use -f:

tail -n 50 -f application.log

By default, GNU tail follows the opened file descriptor. If rotation renames that file and creates a new file at the same path, descriptor following can remain attached to the old file. GNU tail's -F follows the filename and retries when it is removed or temporarily unavailable. tail -F application.log is useful for a log that is renamed and recreated. -F is a GNU extension; check the local manual before using it in a portable script.

No new output does not prove that the application stopped. It may be writing to another path, buffering records, or keeping the old file open. Confirm the configured pathname, process, file timestamps, and rotation method. If the active output is a system journal rather than a normal file, use a log reader such as journalctl.

tail can filter a stream without creating an intermediate file. For instance, journalctl -u example.service | tail -n 40 displays the last forty lines of that command's output, though the first command may still produce a large stream. Stop a follow process when it is no longer needed, especially if many files or users are being monitored.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Skillset Skyeng Senior PlatOps preview
image descriptionSkyeng Senior PlatOps
by Artem Naumenko (slack2)
7 users
Skillset Skyeng Ops (for DevOps) preview
image descriptionSkyeng Ops (for DevOps)
by Roman Burnashev
2 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful