How to truncate all logfiles?

try this:

truncate -s 0 /var/log/*log

EDIT:

if you want to do this more than once you should use logrotate to handle your logs. Usually it's installed in ubuntu. Have a look at man logrotate (or if you do not have it installed look at the online manpage or install it with sudo apt-get install logrotate)

from the manpage:

logrotate 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.


If you want to clear all your log files, not just those in the first-level log folder, you could use:

shopt -s globstar                  # if needed
truncate -s 0 /var/log/*.log       # first-level logs
truncate -s 0 /var/log/**/*.log    # nested folders, like /var/log/nginx/access.log

Noting that if you already have logrotate running, you'll need to clear out the rotated .gz logs as well:

find /var/log -type f -name '*.[0-99].gz' -exec rm {} +

A valid use for this could be building a VM appliance container for distribution, for example.

You should not need to do this as part of routine maintenance: as D-E-N quite correctly suggested, use logrotate for that.


As follow up to @D-E-N answer

This will find all log files in /var/log and truncate them to 0 bytes.

find /var/log -type f -iname '*.log' -print0 | xargs -0 truncate -s0