Coder
Coder

Reputation: 1899

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> *.

Upvotes: 6

Views: 11191

Answers (3)

legends2k
legends2k

Reputation: 32904

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.

Upvotes: 10

Julio Viana
Julio Viana

Reputation: 86

This command takes into account the number of lines above also

tail -f -n +1 yourfile.txt | nl

Upvotes: 7

Arnab Nandy
Arnab Nandy

Reputation: 6702

You can use less command,

tail -f <filename> | less -N

According to man page of less

-N or --LINE-NUMBERS Causes a line number to be displayed at the beginning of each line in the display.

Upvotes: 0

Related Questions