Unix command to empty logfile from within "less"?

4

I prefer using "less" to reading logfiles. As opposed to "tail -f" I can both follow (Shift + F), stop (Ctrl + C), and navigate up and down u and d, etc.

Now what I really would like is the ability to empty a certain logfile from within the program. Is that possible?

Jesper Rønn-Jensen

Posted 2009-12-18T09:05:34.333

Reputation: 5 450

Answers

5

The easiest way IMHO is:
!>%
That is: Exception mark "!", greater sign ">"and percentage sign "%".
Maybe it helps.

Wolf

Posted 2009-12-18T09:05:34.333

Reputation: 2 425

could you provide a short explanation of how it works? I am not particularly familiar with either of the signs? – Jesper Rønn-Jensen – 2010-01-23T23:55:59.753

1I'll try. My english is not so great, though. And since we have little space for remarks, it will have to be split. Here we go: "!"/Exclamation mark invokes a shell to run what we type next (see man less). ">"/greater sign is the shell redirection command (see man bash). "%"/Percentage sign is the name of the current file (see man less). So in simple words we tell less to "start a shell (!) and redirect (>) to this file (%)". But what does it redirect? Since no file is given, it redirects "standard input" to the file. Standard input is empty, so "nothing" is redirected to the file... – Wolf – 2010-02-01T08:37:39.067

... so the file - in the end - has "nothing". It's empty. You can clean up all sorts of files that way. just type ">filename" on the shell prompt to empty the file "filename". See also "man bash" under "Redirecting Output" (bash has a long manpage...) Hope I could help. – Wolf – 2010-02-01T08:40:38.170

@Wolf why don't you edit that explanation directly into the answer? – Michael – 2015-06-03T19:50:41.477

+1 Nice trick. Also nicely cryptic ;-). – sleske – 2010-07-01T20:47:53.500

2

You could simply press v to edit the file in vim. In vim you should end up on the line that you last were on in less, thus pressing Vggd should delete from your current position to the top of the file. With :wq you should get back into less.

However, the application that writes the log file may not like that at all, so your mileage my vary.

innaM

Posted 2009-12-18T09:05:34.333

Reputation: 9 208

1

The easiest I can think of is probably to use ^Z to put the current process in the background, remove your files, and type fg to go back to your process. That said, removing log files is not the best way to deal with them, it's much better usually to use logrotate to compress them.

ℝaphink

Posted 2009-12-18T09:05:34.333

Reputation: 3 531

0

You could use !cp /dev/null %, to truncate the currently viewed file. But as others have noted, it may not work correctly (e.g. if the application writing to the file holds the file open instead of opening/writing/closing for each entry). You might also try !mv -f % %.old && cp /dev/null %, which will move the file out of the way then recreate it.

Either way, I see some strange behavior from less when trying these. (R does not refresh from disk, G does not seek to and display the current EOF (usually used to “pull in” new lines without going into ‘tail mode’), hqG reads in new lines from the file, but appends the entire contents of the file to the end of the buffer instead of just adding the new lines to the end (file has lines 1, then 1/2, then 1/2/3, etc. over time, less might show 1/1/1/2/1/2/1/2/3/1/2/3/1/2/3/4/1/2/3/4/1/2/3/4/5 if I was doing “hqG” twice after every new line)).

These same problems seem to happen with the “use v to truncate-by-editor” method, too.

Chris Johnsen

Posted 2009-12-18T09:05:34.333

Reputation: 31 786