如何在使用inotifywait时指定要监控的文件扩展名?
解决inotifywait指定监控特定扩展名文件的问题
你之前的写法错误是因为inotifywait的最后一个参数是监控路径,不能直接放通配符来过滤文件。要实现特定扩展名的监控,有两种靠谱的方式:
方法一:用inotifywait自带的--include正则匹配
直接让inotifywait只监听符合扩展名条件的文件,修改代码如下:
cd "tmp" inotifywait -me close_write --format %f --include '\.(tmp|tmp2)$' . | while read file do rclone move "$file" … done
这里--include '\.(tmp|tmp2)$'是用正则匹配文件名,意思是只匹配以.tmp或.tmp2结尾的文件。注意正则需要用引号括起来,避免shell解析。
方法二:在循环内判断文件扩展名
如果需要更灵活的逻辑(比如后续要加其他判断),可以在读取到文件名后再过滤:
cd "tmp" inotifywait -me close_write --format %f . | while read file do case "$file" in *.tmp|*.tmp2) rclone move "$file" … ;; *) # 非目标扩展名的文件直接跳过 continue ;; esac done
内容的提问来源于stack exchange,提问作者God of Money
相关产品推荐
相关产品推荐

