Empty or Delete a log files in Linux or UNIX

See all GNU/Linux related FAQ
My web server’s log file size is quite huge or large. How do I delete a log file in Linux without disturbing running application? Is there a proper way to clear log files on Unix?

You can simply truncate a log file using > filename syntax. For example if log file name is /var/log/foo, try > /var/log/foo as root user.

How to clear the contents of a log file from the command line

Say you want to clear the contents of a log file named /var/log/messages, run:
# >/var/log/messages
The following is compatible with various Linux or Unix shells:
: > /var/log/messages
Verify file size:
# ls -l /var/log/messages
If you really wanted to delete or remove a file type the following rm command:
# rm /var/log/message

Delete a log files in Linux or UNIX using truncate

Use the truncate command to shrink or extend the size of each FILE to the specified size. So a proper way to clear log file named www.cyberciti.biz_access.log is to run the following command:
# cd /var/log/nginx/
# ls -lh www.cyberciti.biz_access.log
# truncate -s 0 www.cyberciti.biz_access.log
# ls -lh www.cyberciti.biz_access.log

How to delete a log files in Linux or UNIX using the bash shell

Clear the contents of a file named /var/log/nginx/www.cyberciti.biz_access.log from the command line

Other commands to empty or delete a large file content in Linux

Try the cat command:
cat /dev/null > www.cyberciti.biz_access.log
Or the cp command:
# cp /dev/null /var/log/nginx/php_error.log

How do I clear log file using dd on Linux or Unix?

Type dd command as follows:
# dd if=/dev/null of=/path/to/log/file
# dd if=/dev/null of=/var/log/lighttpd/error_log

How to empty or truncate a file in Linux using echo/printf

One more method is to use the echo command:
# echo -n "" > /path/to/java/appserver.log

Say hello to 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 my previous logrotate help page:


This page showed how to empty or delete a large log file contents in Linux and Unix like systems. There are other methods too as discussed in the comments section below.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

12 comments… add one
  • vb Jun 16, 2007 @ 3:39

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

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Jun 16, 2007 @ 13:31

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

  • dave Jul 19, 2007 @ 15:23

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

    • Vit Nov 23, 2014 @ 10:49

      One way is to setup new cronjob for that. Then google: “tmpwatch delete files older”

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Jul 19, 2007 @ 20:49

    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

  • dave Jul 23, 2007 @ 16:42

    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 {
    rotate 30
    /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/”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.

  • chandra Dec 17, 2008 @ 7:35

    simply wirte rm file.log and press enter

    • Ajeet Jha Sep 24, 2010 @ 10:21

      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.

      • indra Feb 19, 2013 @ 3:17

        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

  • Anand Shankar Jun 28, 2012 @ 13:11

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

  • Ratnakar Jul 10, 2012 @ 14:33

    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

  • rahul Mar 4, 2014 @ 7:04

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.