67

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?

5
  • 2
    You could try with less --follow-name or less --follow-name +F Apr 14, 2015 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! Apr 14, 2015 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. Apr 15, 2015 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. Apr 15, 2015 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! Aug 15, 2017 at 8:27

3 Answers 3

76

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
6
  • 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. May 3, 2017 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. May 8, 2017 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. May 10, 2017 at 7:02
  • My favorite less command to follow a growing rotating log file is this: less -N --follow-name +F ~/cpu_log.log. The -N also adds line numbers. I've now cited your answer and added --follow-name to my answer here. Jan 20, 2022 at 23:20
  • So pressing F makes less refresh the file?
    – HappyFace
    Jan 30, 2022 at 11:11
7

Also you can activate the follow mode if you call less filename like normal and then press Shift + F. With Ctrl + C you can then deactivate follow mode again.

But be aware that by default this will not work in an alpine docker image.

6

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

7
  • 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. Aug 23, 2018 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. Aug 23, 2018 at 12:08
  • Yes I am sure, but go ahead and down vote me, stackexchange needs to share the love Aug 23, 2018 at 12:08
  • 1
    Just to be sure you did think I down voted you: No, I upvoted you. Aug 23, 2018 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .