37

The command less can be used to replace tail in

tail -f file

to provide features like handling binary output and navigating the scrollback:

less +F file

The + prefix means "pretend I type that after startup", and the key F starts following.

But can less also replace

tail --follow=name file

which follows file even if the actual file gets deleted or moved away, like a log file that is moved to file.log.1, and then a new file is created with the same name as the followed file?

  • 1
    You could try with less --follow-name or less --follow-name +F – don_crissti Apr 14 '15 at 20:41
  • @don_crissti Nice... less --follow-name +F is it - no idea how I missed that - I was pretty sure I searched the man page for follow... strange. Make it an answer! – Volker Siegel Apr 14 '15 at 23:48
  • Well, some more searching reveals it's already been asked and answered here so I'll leave it as a comment. You can always upvote the answer there. – don_crissti Apr 15 '15 at 0:35
  • @don_crissti Right... except that your correct answer is not found there - --follow-name is described, but +F is never mentioned, and I think it's pretty important because it's not widely known obviously. – Volker Siegel Apr 15 '15 at 0:45
  • If you came here because of a Google search for less follow, READ THE QUESTION. It already answers how to make less follow file changes! – Hubert Grzeskowiak Aug 15 '17 at 8:27
37

Yes, less can follow by file name

The feature has a fairly obscure syntax:

less --follow-name +F file.log

With less, --follow-name is different from the tail option --follow=name.
It does not make less follow the file, instead it modifies the behaviour of the command key F inside of less to follow based on the file name, not the file descriptor.

Also, there is no normal option to start less in follow mode.
But you can use the command line to give keystrokes to execute after startup, by prefixing them with +.
Combining the modifier option with +F, less will actually start in the (modified) follow mode.

Use +F alone for the equivalent of plain tail -f:

less +F file.log
  • I suspect that if you follow an infinite stream with less, this will eventually exhaust your memory because unlike tail -f, the amount of memory used is not limited by the terminal scrollback limit (unless the terminal scrollback limit is also infinite). less +F has to preserve the entire scrollback history, and cannot rely on reloading slices from a file on the harddisk. – CMCDragonkai May 3 '17 at 2:31
  • @CMCDragonkai Interesting point. less has options to control the buffer size used per file (-b and -B), it's not obvious to me whether the size is limited by default. – Volker Siegel May 8 '17 at 18:04
  • The man page for -B says that "By default, when data is read from a pipe, buffers are allocated automatically as needed. If a large amount of data is read from the pipe, this can cause a large amount of memory to be allocated. The -B option disables this automatic allocation of buffers for pipes, so that only 64 K ( or the amount of space specified by the -b option) is used for the pipe.". So that means when reading from a infinite pipe, infinite memory is allocated by default, but if reading from a file like a log file, then 64 K slices is used by default. – CMCDragonkai May 10 '17 at 7:02
3

In Fedora at least less has a +F option that follows the contents of a file just like tail -f does..

Update, try hitting F in less to toggle to follow mode as well

  • Are you sure? My less has a conflicting option named the same, -F: The man page of less says: -F or --quit-if-one-screen. Which version of less do you have? Mine says less 458 with the option -V. – Volker Siegel Aug 23 '18 at 11:59
  • Very interesting, thanks for the link! That seems not to be a gnu less. The full first line of less -V is less 458 (GNU regular expressions). Please give me your version. – Volker Siegel Aug 23 '18 at 12:08
  • Yes I am sure, but go ahead and down vote me, stackexchange needs to share the love – user1529413 Aug 23 '18 at 12:08
  • 1
    Just to be sure you did think I down voted you: No, I upvoted you. – Volker Siegel Aug 23 '18 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.