linux find命令妙用
对于需要查找一定时间的文件,可以使用find 命令处理
格式参考
find /path/to/search -type f -name "pattern" -mtime -2 -exec sh /path/to/XXX.sh {} /;
更多细节讲解看这里find命令时间细讲
那么实际工作中使用的案例
核心命令
find /var/path/ -type f -name 'filename' -mtime -2 -exec sh yourShellScript.sh {} /;在某个文件夹下面查找某个文件
定义一堆路径,然后每两分钟循环查找,找到文件就执行你的shell脚本,找不到就不执行
#!/bin/bash
auto_sys_path=/apath/
tgt_path=/another/path/
scr_path=$auto_sys_path/scriptpath
log_path=$auto_sys_path/logspath
while true; doecho "$(date): 执行查找" >> ${log_path}/find_execute.logfind $(tgt_path) -type f -name 'ur filename' -mtime -5 -exec sh ${scr_path}/yourShellScript.sh {} /;sleep 120 # 120秒 = 2分钟
done