如何使用inotify自动删除linux中创建的文件?
发布时间:2020-12-14 01:03:35 所属栏目:Linux 来源:网络整理
导读:我试图用inotify删除创建的文件,但它不起作用: inotifywait -r --format '%w%f' -e create /test rm $FILE 当我在/ test中创建一个文件时,我得到了这个: /test/somefile.txtrm: missing operandTry `rm --help' for more information. 所以似乎$FILE变量没
我试图用inotify删除创建的文件,但它不起作用:
inotifywait -r --format '%w%f' -e create /test && rm $FILE 当我在/ test中创建一个文件时,我得到了这个: /test/somefile.txt rm: missing operand Try `rm --help' for more information. 所以似乎$FILE变量没有传递给rm命令……我怎么能正确地做到这一点?谢谢. 解决方法
启动inotifywait一次(没有-m标志)时,您可以轻松使用
xargs:
inotifywait -r --format '%w%f' -e create /test -q | xargs /bin/rm 将等待/ test中的文件创建,将文件名提供给xargs并将此arg提供给/ bin / rm以删除该文件,然后它将退出. 如果需要连续观察目录(使用inotifywait的-m参数),请创建如下脚本文件: inotifywait -m -r --format '%w%f' -e create /test | while read FILE do /bin/rm $FILE done 然后,将删除在您/ test目录中创建的每个新文件. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |