【问题标题】:how to automatically delete created file in linux with inotify?如何使用inotify自动删除linux中创建的文件?
【发布时间】:2011-11-29 17:18:32
【问题描述】:

我正在尝试使用 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 命令...我怎样才能正确地做到这一点?谢谢。

【问题讨论】:

  • 我希望你不要这样做来获取临时文件!要获取临时文件,只需在其目录中添加unlink 条目,同时仍然具有open-ed 文件描述符......这就是tmpfile 所做的。

标签: linux inotify


【解决方案1】:

启动一次 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 目录中创建的每个新文件都将被删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多