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?


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

  • 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
  • 1
    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
  • @Wolf why don't you edit that explanation directly into the answer? – Michael Jun 3 '15 at 19:50

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.


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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.