-
查找目标文件并更新为当前时间
find /目标路径 -type f -name "*.log" -exec touch -a -m {} +
-type f
限定普通文件-name "*.log"
匹配日志文件-exec
执行touch命令-a -m
同时更新访问时间和修改时间
-
指定精确时间戳修改
find /目标路径 -mtime +30 -exec touch -t 202310011200 {} \;
-mtime +30
查找30天前的文件-t 202310011200
格式:年月日时分
-
同步参考文件时间戳
find /目标路径 -newer reference.txt -exec touch -r reference.txt {} +
-newer
查找比参考文件新的文件-r
继承参考文件的时间属性
注意事项:
① 先用find -print
验证匹配结果
② 特殊字符路径需添加-print0 | xargs -0
③ 需具备目标文件的写入权限