5

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

4

1 回答 1

6

When launching your inotifywait once (without the -m flag), you can easily use xargs :

inotifywait -r --format '%w%f' -e create /test -q | xargs /bin/rm

that will wait for a file creation in /test, give the filename to xargs and give this arg to /bin/rm to delete the file, then it will exit.

If you need to continuously watch your directory (with the -m param of inotifywait), create a script file like this :

inotifywait -m -r --format '%w%f' -e create /test | while read FILE
do
        /bin/rm $FILE
done

And then, every newly file created in you /test directory will be removed.

于 2011-11-29T17:28:12.900 回答