The linux tail command displays the last lines of a file. It can also follow a file stream and show the contents updated in the stream.
In the simplest form it displays last ten lines of a file.
Sample syntax of Linux tail command
# tail <file name>
An example of tail command
# tail newdoc
Simplest form of linux tail command
Look at the above snapshot, last ten lines of the file ‘newdoc’ is displayed with the help of command tail newdoc.
You can try another practice with tail command. Because it is the most important thing for handling big file. There are several arguments to use the tail command effectively. The most common is the -f argument. Which follows the file whenever something get written in the file.
Linux tail -f command
The -f argument follows the file. Suppose there is a log file named apache.log, and you want to see what is being written in the file. So if you type tail -f apache.log, you’ll see that, the command didn’t exit. It stays there. Whenever something gets written in the log file, it will also display here.
To get a better understanding, open two terminal. In the first terminal, type nano my-file.txt and hit enter. Write one line and press enter. The press Ctrl+O and then enter to save the file. In the second terminal write tail -f my-file.txt, probably you’ll only see the first line here. Now in the first window, add another line then save the file using Ctrl+O and enter. Now notice in the second terminal. The second line got automatically populated here! Interesting! Isn’t it?
Linux tail -n command
The Unix and Linux system tail -n <num> option displays the specified number of lines. So by default tail <filename> displays last 10 lines written in the file. But, if we want 20 lines then the command should be tail -n 20 <filename>, or if we want to display only 5 lines, then the command is tail -n 5 <filename>.
Sample syntax of tail -n command :
# tail -n <number> <file name> [ n means number of line ]
Example of the tail -n :
# tail -5 newdoc
linux tail command with specific line count
You can see in the above picture, the last five lines of the file ‘newdoc’ is displayed with the help of “tail -5 newdoc” command. You can show another line number, according to your need. Just change you line number and you file name.
Linux tail -c command
The Linux system tail -c option displays the specified number of bytes from the last. So it will start from the last byte and follow upwards.
Sample syntax of tail -c command :
# tail -c <number> <file name>
Relevant example of tail -c command :
# tail -c 5 newdoc
linux tail command with specific byte count
Look at the above snapshot, the last 12 bytes of file ‘newdoc’ is displayed with the help of “tail -c 5 newdoc” command. You can another practice you own ability.
Linux tail command is the most important command for your reading log files. tail command options are helpful to your for linux file management. Use the command regularly to master it more.
Read our Linux Shell Commands Introduction to get a summary on how commands work in the shell.