68

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?

Volker Siegel
  • 17,283
  • 2
    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 00: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 00: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 08:27

3 Answers3

77

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
Volker Siegel
  • 17,283
  • 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 03 '17 at 02: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 08 '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 07: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. – Gabriel Staples Jan 20 '22 at 23:20
  • So pressing F makes less refresh the file? – HappyFace Jan 30 '22 at 11:11
  • @HappyFace that depends on what you mean with refresh, exactly. Read new data that was added to the file: Yes. But not if you mean reload the whole file or part of it, discarding what you currently have. – Volker Siegel Jan 30 '22 at 11:21
8

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.

7

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
  • https://unix.stackexchange.com/questions/467/the-less-command-line-equivalent-of-tail-f – user1529413 Aug 23 '18 at 12:03
  • 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
  • (mingw) less 481 (PCRE regular expressions) Copyright (C) 1984-2015 Mark Nudelman (fedora) less 487 (POSIX regular expressions) Copyright (C) 1984-2016 Mark Nudelman – user1529413 Aug 23 '18 at 12:09
  • 1
    Just to be sure you did think I down voted you: No, I upvoted you. – Volker Siegel Aug 23 '18 at 12:10
  • And do not take the votes so seriously. Your posts are actually really valuable to me. I know there were lots of conflicts between the unix from HP, HPUX, Simens had one, Sun had two... but now, I did not expect there are still conflicts. – Volker Siegel Aug 23 '18 at 12:15