181

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?

Micromega
  • 4,211

1 Answers1

359
> 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
peterph
  • 30,838
  • What about shred? Can it zero my log files? – Micromega 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? – Micromega 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 https://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators for more details. – peterph Oct 15 '17 at 22:58
  • You are correct @peterph. I'm deleting my previous comment as it contains incorrect information. I would edit it, but it can't be edited so I'm opting for deletion. My previous comment said: "If you want to keep the last N lines of the file you can use tail -n 10000 logfile > logfile". Which is incorrect; don't do it! – Luke Griffiths Oct 19 '17 at 20:24
  • for Freebsd: truncate -s 0 logfile – crea7or Dec 10 '17 at 17:14
  • How can do it to multiple files? $ cat /dev/null > *.log -bash: *.log: ambiguous redirect – Cloud Jan 12 '18 at 03:57
  • @SiminJie see updated answer – peterph Jan 14 '18 at 21:37