Empty or Delete a log files in Linux or UNIX

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 sxi.io_access.log is to run the following command:
# cd /var/log/nginx/
# ls -lh sxi.io_access.log
# truncate -s 0 sxi.io_access.log
# ls -lh sxi.io_access.log

Clear the contents of a file named /var/log/nginx/sxi.io_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 > sxi.io_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.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

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

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts