Take the 2-minute tour ×

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?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

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

share|improve this answer
could you provide a short explanation of how it works? I am not particularly familiar with either of the signs? –  Jesper Rønn-Jensen Jan 23 '10 at 23:55
I'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 Feb 1 '10 at 8:37
... 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 Feb 1 '10 at 8:40
+1 Nice trick. Also nicely cryptic ;-). –  sleske Jul 1 '10 at 20:47
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


