maintain log files by shell

move log file to another device

scheduled move specific file to target directory

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh
# move log 1 month ago

# get date string
date=`date -d '1 month ago' +'%Y-%m-%d'`

# file name
default="/logs/common-default.log."${date}".log"

if [ -f "$default" ]; then
echo $date' move '$default' to /data0/backup' >> /logs/backup.log
`mv $default /data0/backup`
fi

crontab -e add schedule

1
10 0 * * * sh /move.sh

delete log file by function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/sh
# delete log 1 month ago

# get date string
date=`date -d '1 month ago' +'%Y-%m-%d'`

# find files modify 1 month ago
files=find /data0/backup -type f -not -newermt $date

function delete_log_files
{
while test $# -gt 0
do
`rm -f $1`
echo '$date clean $1' >> /logs/cleanup.log
shift
done
}

delete_log_files $files

delete log file by command

1
2
#!/bin/sh
find /logs/ -type f -mtime +30 -name '*.log' -exec rm -rf {} \;

{} is placeholder of the file be found
\; mean exec command finish


process every line in a multi-line variable
https://cloud.tencent.com/developer/ask/46222

‘crontab’ schedule
https://www.cnblogs.com/peida/archive/2013/01/08/2850483.html

‘find’ command -exec
https://blog.csdn.net/u010900754/article/details/83020378

‘find’ command useage
https://blog.csdn.net/h106140873/article/details/72874455