9

I love less, which I can use to follow logs with the +F flag (or the ShiftF hotkey), search forwards and backwards, and generally move freely through the document.

But there is one thing missing in less: usually I am at the end of the file, and I want to see new things happening. In tail -f I would just hit enter several times, and new log lines would just appear clearly separated from old lines.

Is it possible to add this to less? How?

1
  • I don't think you can do this, though sometimes I will do echo -e '\n\n\n\n\n' >> /dev/pts/0 for example, it isn't as clean but atleast lets me have a point of reference where I left off when i look away. Apr 26, 2013 at 10:19

5 Answers 5

6

This isn't really tail's behavior -- it's the terminal. Tail is just sending a stream of output to stdout, which goes to your terminal, and you're inserting blank lines in the middle of that output by hitting enter. Tail itself isn't actually accepting input from you at all. You can type whatever else you want and that will show up too.

less, however, is a console app, and it takes input and processes it. It's managing the console using terminal control sequences, which is how you can scroll around. Letting you insert arbitrary characters into the stream would mess up the display, and anyway less uses your input to control the program.

Unfortunately, it doesn't seem to have a way to mark the current position visually. That's not a terrible feature request, though, and less does still get new features from time to time.

2
4

I found your question while seeking the same answer for myself.

Disappointed by the accepted answer, I came up with a workaround. It's less than ideal, but it allows me to mark my position in the log I'm following with less, which is the whole point.

I created a small executable script (I called it marklog) with the following contents, and put it in my path:

#!/bin/sh
echo >> $1
echo >> $1
echo >> $1
echo `date` ---------------------------------------------------------------------- >> $1
echo >> $1
echo >> $1

Of course, this only works if you have write access to the log file--which could be a deal-breaker in many situations. I've also created this version which I use to write to log files I don't own (but to which I have sudo access):

#!/bin/sh
sudo sh -c "echo >> $1"
sudo sh -c "echo >> $1"
sudo sh -c "echo >> $1"
sudo sh -c "echo `date` ------------------------------------------------------------------------- >> $1"
sudo sh -c "echo >> $1"
sudo sh -c "echo >> $1"

These scripts provide just the sort of visual break I was looking for. There are at least 3 ways you can use them:

  1. At the point where you would normally press enter a few times when using tail -f, instead run marklog from another terminal (providing the path to the log file as an argument).

  2. Use CtrlZ to suspend less so you can run the script in the same terminal window, but when you re-foreground less (using fg, of course), it will no longer be in 'follow' mode, so you'll need to hit ShiftF again...

  3. Lastly--and this might be the most convenient way, because you don't need to type the path to the log file: Run marklog directly from less by typing !marklog %. Less will substitute the current filename for %. However, less won't respond to the ! while it is in 'follow' mode, so you'll have to hit CtrlC first to exit follow mode, run !marklog %, then ShiftF again.

With method 3, you also get the added bonus of of Less's command history: Just hit the ! and then use the up-arrow to select the most recent command (for me, it's always marklog).

Hope this helps someone as much as has already helped me.

1

Press Shift+F at the end of file and less will start to work as tail -f.

1
  • 2
    No, it won't. It will follow new lines, but it will not work as tail -f. Specifically it will not accept enter as newlines, which is the only point of my question. Besides, I already mentioned Shift+F in the question (with a small error, I must say)
    – blueFast
    Apr 26, 2013 at 10:10
1

I don't think you can do this. I've used m and ' to mark places of interest and then goto them again but this doesn't give you the separation that you ask for. Perhaps something like multitail will do what you want.

-1

The answer from Lambart brings me to my favorite solution. Thx to Lambart!

Just interput the follow mode ctrlc and enter !printf "\n\n---$(date)---\n\n" >> %. After that you have to press enter one more time and hit shiftf to start again following the file.

You must log in to answer this question.

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