Sunday, April 28, 2024
47
rated 0 times [  47] [ 0]  / answers: 1 / hits: 61451  / 1 Year ago, sun, march 19, 2023, 11:30:00

can anybody give me a solution to truncate all logfile in /var/log/ directory?



and a question just for knowledge, is it a good idea or not?



#!/bin/bash
LOGDIR="/var/log"
for logfile in $(ls $LOGDIR/*log)
do
truncate -s 0 $logfile
done

More From » command-line

 Answers
1

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.



[#32263] Monday, March 20, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cugure

Total Points: 188
Total Questions: 110
Total Answers: 103

Location: Dominican Republic
Member since Sun, Sep 4, 2022
2 Years ago
;