Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Is there a better way to clean the log file? I usually delete the old logfile and create a new logfile and I am looking for a shorter type/bash/command program. How can I use an alias?

share|improve this question

marked as duplicate by slm, jasonwryan, rahmu, terdon, Anthon Sep 26 '13 at 4:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 166 down vote accepted
> logfile


cat /dev/null > logfile

if you want to be more eloquent, will empty logfile (actually they will truncate it to zero size). If you want to know how long it "takes", you may use

dd if=/dev/null of=logfile

(which is the same as dd if=/dev/null > logfile, by the way)

You can also use

truncate logfile --size 0

to be perfectly explicit or, if you don't want to,

rm logfile

(applications usually do recreate a logfile if it doesn't exist already).

However, since logfiles are usually useful, you might want to compress and save a copy. While you could do that with your own script, it is a good idea to at least try using an existing working solution, in this case logrotate, which can do exactly that and is reasonably configurable.

share|improve this answer
What about shred? Can it zero my log files? – Betterdev Sep 25 '13 at 20:37
What exactly do you want to achieve? shred will destroy the data, so that it will be hard to recover them even with forensic tools (from a spinning plate HDD, that is), but it will take it ages (it overwrites the blocks used by the file several times). > logfile will just truncate it (very quickly). – peterph Sep 25 '13 at 20:45
@Phpdna logrotate has a shred option. Use logrotate. – Sammitch Sep 25 '13 at 22:04
I don't have much space left. Is this something new? – Betterdev Sep 25 '13 at 22:09
thanks for the solutions – Amit Nov 13 '15 at 18:22

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