删除超过 30 天的文件的脚本

Jay*_*n T 5 linux redhat shell syslog

Shell 脚本不应删除* root 目录* 下的任何文件。我的* 路径将类似于 /export/home/ftp/ ...

我做了一些研究,并找到了使用 find 和 exec 命令从特定路径查找和删除超过 30 天的文件的方法。

*find /export/home/ftp/ -type f -mtime +30 -exec rm -f {} \;

但根据要求,我只想从该目录中删除 console.log 和 server.log 并排除其余文件。

请帮我解决这个问题。

Sam*_*cke 10

如果您只需要每个月删除旧的 server.log 和 console.log,您也可以使用logrotate最有可能已经在 RHEL 下运行的。像这样的配置片段将在/etc/logrotate.d/*.conf配置文件位于系统上的任何位置或任何位置工作。

# rotate server.log and console.log every month
# delete, not compress, old file

/export/home/ftp/server.log /export/home/ftp/console.log {
    monthly
    rotate 0
}
Run Code Online (Sandbox Code Playgroud)

如上所述,自定义的每月 cron 也可以很好地工作。事实上,由于 logrotate 是从 cron 运行的,您可以将其视为各种 cron 扩展。哈。


jgo*_*afe 5

假设您确实需要使用find以通过子目录递归:

find /export/home/ftp \( -name console.log -or -name server.log \) -mtime +30 -exec rm -f {} +
Run Code Online (Sandbox Code Playgroud)