166

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?

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.

333
> logfile

or

cat /dev/null > logfile

or

true | tee logfile

(fell free to substitute false or any other command that produces no output, like e.g. : does in bash) if you want to be more eloquent, will all 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 --size 0 logfile

(or truncate -s 0 logfile) to be perfectly explicit or, if you don't want to,

rm logfile

(in which case you are relying on the common behaviour that 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.

Should you need to do it for several files, the safe way is

truncate -s 0 file1 file2 ...

or

> file1 > file2 ...

Some shells (zsh) also allow one to specify several redirection targets.

This works (at least in bash) since it creates all the redirections required although only the last one will catch any input (or none in this case). The tee example with several files should work in any case (given your tee does know how to handle several output files)

Of course, the good old shell loop would work as well:

for f in file1 file2 ... ; do
    # pick your favourite emptying method
done

although it will be much slower due to the command being run separately for each file. That may be helped by using find:

find <criteria matching required files> \
    -exec <command capable of zeroing several files> {} \+

or

find <criteria matching required files> -delete
  • What about shred? Can it zero my log files? – Gigamegs 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? – Gigamegs Sep 25 '13 at 22:09
  • @LukeGriffiths that will actually truncate the file as well, for it will be opened by the shell before the command is spawned with the O_TRUNC flag. See unix.stackexchange.com/questions/159513/… for more details. – peterph Oct 15 '17 at 22:58

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