在 Linux 中删除早于 “X” 天的文件夹的 Bash 脚本
我们有一个名为 /var/log/app/ 的文件夹,其中包含 15 天的日志,我们将删除早于 10 天的文件夹。
$ ls-lh /var/log/app/
drwxrw-rw-3 root root 24KOct123:52 app_log.01
drwxrw-rw-3 root root 24KOct223:52 app_log.02
drwxrw-rw-3 root root 24KOct323:52 app_log.03
drwxrw-rw-3 root root 24KOct423:52 app_log.04
drwxrw-rw-3 root root 24KOct523:52 app_log.05
drwxrw-rw-3 root root 24KOct623:54 app_log.06
drwxrw-rw-3 root root 24KOct723:53 app_log.07
drwxrw-rw-3 root root 24KOct823:51 app_log.08
drwxrw-rw-3 root root 24KOct923:52 app_log.09
drwxrw-rw-3 root root 24KOct1023:52 app_log.10
drwxrw-rw-3 root root 24KOct1123:52 app_log.11
drwxrw-rw-3 root root 24KOct1223:52 app_log.12
drwxrw-rw-3 root root 24KOct1323:52 app_log.13
drwxrw-rw-3 root root 24KOct1423:52 app_log.14
drwxrw-rw-3 root root 24KOct1523:52 app_log.15
该脚本将删除早于 10 天的文件夹,并通过邮件发送文件夹列表。
你可以根据需要修改 -mtime X 的值。另外,请替换你的电子邮箱,而不是用我们的。
# /opt/script/delete-old-folders.sh
#!/bin/bash
prev_count=0
fpath=/var/log/app/app_log.*
find $fpath -type d -mtime +10-execls-ltrh {} \; >/tmp/folder.out