Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tail -f <filename>, print line number as well

Is there a way to modify so that the tail -f lists the line number of the current file as well.

Something similar to grep -n <Strings> *.

like image 625
Vikas Goel Avatar asked Jan 20 '15 10:01

Vikas Goel


People also ask

What is the default number of lines printed by tail?

Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits.

How do you find the tail number of a line?

The tail command is used to print last 10 lines of a file by default. However, like the head command, we can change the number number of lines to be displayed by using the -n option, or just -<number> , to display a different number of lines as specified.

How do you write a tail command that will print the last 10 lines of a file?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.


1 Answers

Try less

Instead of using tail to follow data and less or nl for numbering, I suggest using a single tool for both:

less -N +F <filename>

This will make less print line numbers and follow the file. From man less:

F

Scroll forward, and keep trying to read when the end of file is reached. Normally this command would be used when already at the end of the file. It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the tail -f command.)

You could do a Ctrl+C to stop following when inside less; to start following again, you could press F again. With this method, you get the additional goodies that less offers like regex-based search, tags, etc.

like image 109
legends2k Avatar answered Nov 15 '22 11:11

legends2k