tail Command: Tutorial & Examples
Display the last few lines of a file
The tail
command is a utility that is used to display the last few lines of a text file. It is often used to view log files and other files that are constantly being updated, as
it allows you to see the most recent entries in the file.
To view the last 10 lines of a file with tail
, simply type tail
followed by the name of the file:
tail file.txt
This will display the last 10 lines of the file. You can use the -n
flag followed by a number to specify the number of lines to display:
tail -n 20 file.txt
This will display the last 20 lines of the file.
You can use the -f
flag to follow the file and display new lines as they are added to the file:
tail -f file.txt
This is useful for monitoring log files and other files that are constantly being updated.
tail
is a simple and useful tool for viewing the end of text files. It is often used in combination with other commands, such as grep
or less
, to filter and process the output of those commands before viewing it in tail
.
Further Reading
- Bash Cookbook by Carl Albing, J.P. Vossen
- Wicked Cool Shell Scripts by Dave Taylor, Brandon Perry
- Black Hat Bash by Nick Aleks, Dolev Farhi
- Bash Pocket Reference by Arnold Robbins
- The Linux Command Line by William Shotts
- Learning the Bash Shell by Cameron Newham
- Mastering Linux Shell Scripting by Mokhtar Ebrahim, Andrew Mallett
- Linux Command Line and Shell Scripting Bible by Richard Blum, Christine Bresnahan
- Shell Scripting by Jason Cannon