Delete a log files in Linux or UNIX

by on February 2, 2007 · 11 comments· LAST UPDATED February 2, 2007

in , ,

Q. How do I delete a log file in Linux without distributing running application? My log file size is quite huge.

A. You can simply truncate a log file using > filename syntax. For example if log file name is /var/log/message, use:
# >/var/log/message

If you really wanted to delete or remove a file type:
# rm /var/log/message

logrotate tool

A better approach is to use logrotate tool. It is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

See previous logrotate faq article

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 vb June 16, 2007 at 3:39 am

my log files /var/log deleted is it creates any problem

Reply

2 nixCraft June 16, 2007 at 1:31 pm

/var/log is directory, if you delete the same just recreate directory using mkdir command:
[code]mkdir /var/log[/code]

Reply

3 dave July 19, 2007 at 3:23 pm

How would you specify to delete log that are over a certain age. ie 2days old or something like that. Thanks for the help.

Reply

4 nixCraft July 19, 2007 at 8:49 pm

There are 2 options, either use log logrotate or use find command to get list of 2 days old files and empty them. You can find information about logrotate and about find command here only. Use search box to get information

Reply

5 dave July 23, 2007 at 4:42 pm

Thank you for pointing me in the right direction. I’ve been doing some more research and had another question if you wouldn’t mine throwing in some input.

I want to remove apache logs after about a month. So I was going to set something up like this that I found online.

This is an example of a /etc/logrotate.d/httpd

/var/log/httpd/*.log {
daily
rotate 30
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true endscript
}

Now from what I understand this will rotate the log daily meaning create a new log everyday. The 30 means that it will hold 30 of these logs before deleting them (or compressing if I put that option in there)

Also, before I go and set this up, I want to test it on 1 websites log incase I mess things up. Would it be ok to replace the line

/var/log/httpd/*.log
with
/var/log/httpd/”website”*.log to that affect to test in one website before doing all of them?

Does it sound like I’m on the right track or am I completely lost :) thanks again for your input.

Reply

6 chandra December 17, 2008 at 7:35 am

simply wirte rm file.log and press enter

Reply

7 Ajeet Jha September 24, 2010 at 10:21 am

Use the following command:

suppose your file name is message.log then use:

echo “” > message.log

This will empty the file without having deleting it.

Reply

8 indra February 19, 2013 at 3:17 am

Hallo, i have same use command as your suggest.
echo ”> message.log

but, if one folder contain much log like that, how i can clean up directly using echo command without delete it (rm)

Thank you

Reply

9 Anand Shankar June 28, 2012 at 1:11 pm

Hi Ajeet,
Can you tell me please how it works ?

Reply

10 Ratnakar July 10, 2012 at 2:33 pm

echo “” ->print the blank space
and that blank space is redirected to message.log file
or in other word your file conatins replaced by blank space without

Reply

11 rahul March 4, 2014 at 7:04 am

Please could anybody help me how to delete the log files of DHCP server. and how to identify them.

Reply

Leave a Comment

Tagged as:

Previous Faq:

Next Faq: