Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

Currently my log files get very big. Is there a command to empty the files without delete and restore it? It involves 2 command which is sometimes taking too long.

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.

1 Answer 1

up vote 39 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).

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? –  Phpdna 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? –  Phpdna Sep 25 '13 at 22:09
This is such a beautiful answer I don't know how to express it. "cat /dev/null > logfile" worked flawlessly. –  JohnMerlino Jul 29 at 14:42

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