Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Can I delete everything in /var/log? Or should I only delete files (recursively) in /var/log but leave folders?

Does anyone have a good rm command line? (My admin skills leave me nervous.)

Note: I am using Debian. I am not sure what version.

share|improve this question
2  
Deleting log files is a bad idea (you'll also need to find every running process that has it's own log file and "kill -HUP" it, a soft restart that will result in the program recreating any necessary log files). I would strongly advise against deleting log files, rely on utilities like logrotate to manage the contents of /var/log for you automatically (it does stuff like HUP the processes) If I may I'd like to tackle this from a different angle. What problem are you trying to resolve that's led you to consider this? –  Twirrim Sep 29 '10 at 17:34

5 Answers 5

up vote 11 down vote accepted

Instead of deleting the files you should rotate them, e. g. using logrotate.

You never know when you'll actually need the logs from some time ago, so it's better to archive them (up to a reasonable age, e. g. 3 months).

logrotate can compress your old log files so they don't occupy a lot of disk space.

share|improve this answer
2  
logrotate can also delete the oldest files. –  Kevin M Sep 28 '10 at 12:13
2  
Well, IMHO deleting all logs can make perfect sense in some cases. For example I want to build a Virtial Machine image to be used for new deployments. Needless to say I would like it to be a really clean system without any logs, histories, caches etc. saved. –  Ivan Oct 15 '12 at 18:54
    
Sorry, but looking at three months old log files is archeology. If you collect logs to identify problems, then evaluate them quickly. –  countermode Aug 14 at 14:40
    
@countermode You are never in the mood for nostalgia? Like looking at the 3 month old log files thinking about good ol' times? –  Broco Aug 14 at 14:51

If you delete everything in /var/log, you will most likely end up with tons of error messages in very little time, since there are folders in there which are expected to exist (e.g. exim4, apache2, apt, cups, mysql, samba and more). Plus: there are some services or applications that will not create their log files, if they don't exist. They expect at least an empty file to be present. So the direct answer to your question actually is "Do not do this!!!".

As joschi has pointed out, there is no reason to do this. I have debian servers running that haven't had a single log file deleted in years.

share|improve this answer
    
I didnt realize that. good to know. +1 + changed my accept. –  acidzombie24 Sep 28 '10 at 21:31

I'm cloning virtual machines from a master. It makes perfect sense to clear the log on the master so that when you boot the clones you won't get the master's log. I did in tcsh:

cd /var/log
foreach ii ( `find . -type f` )
foreach? cp /dev/null $ii
foreach? end

which clears the logs but keeps the files.

share|improve this answer
    
This should be restricted to a use-case like you describe. –  Sven Nov 9 '12 at 10:41

Delete all files:

find /var/log -type f -delete

Delete all .gz and rotated file

find /var/log -type f -regex ".*\.gz$"
find /var/log -type f -regex ".*\.[0-9]$"

Try run command without "-delete", to test it.

share|improve this answer

You can use the option ctime to find old files... for example:

find -ctime +30

As bindbn explain, first try the find fetch files and after use the option delete :D

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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