summaryrefslogtreecommitdiffstats
path: root/clean-logs.sh
blob: ebfcf36d59653b3e0c4dda85fb7006659473d888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash

#
# ToDo
# truncate -s 0 /var/log/syslog
# /var/log/journal/f* (new in 18.10)
#

# clean traditional logs etc.
find /var/log/ -type f -name "*.gz" -exec rm -f {} \;
find /var/log/ -type f -name "*.1" -exec rm -f {} \;
find /var/log/ -type f -name "*.old" -exec rm -f {} \;

# rhel like logs with 8 digit date at the end
find /var/log -type f -regextype egrep -regex ".*-[0-9]{8}$" -exec rm -f {} \;

# clean systemd's journal
if [[ -d /var/log/journal || -d /run/log/journal/ ]]; then
    systemctl stop systemd-journald
    journal_dirs1=$(find /var/log/journal/ -maxdepth 1 -type d -name "a*")
    journal_dirs2=$(find /run/log/journal/ -maxdepth 1 -type d -name "f*")
    journal_dirs3=$(find /run/log/journal/ /var/log/journal/ -maxdepth 1 -type d -name "*[0-9]*" 2> /dev/null)
    journal_dirs="$journal_dirs1 $journal_dirs2 $journal_dirs3";

    for j in $journal_dirs; do
        echo "removing ${j}"
        rm -rf $j
    done
    systemctl start systemd-journald
fi

# clean atop logs
if [[ -d /var/log/atop ]]; then
    systemctl stop atop
    rm -f /var/log/atop/*
    systemctl start atop
fi

# clean samba logs
if [[ -d /var/log/samba ]]; then
    #rm -rf /var/log/samba/*
    if [[ -d /var/log/samba/old ]]; then
      rm -rf /var/log/samba/old/*
    fi
    find /var/log/samba/* -maxdepth 0 -not -path "/var/log/samba/old" -exec rm -rf {} \;
fi

# at last truncate all *.log
find /var/log/ -name "*.log" -exec truncate -s 0 {} \;

exit 0