How to automatically delete created file in linux using inotify?

I am trying to delete the created file using inotify, but it does not work:

inotifywait -r --format '%w%f' -e create /test && rm $FILE

when I create the file in / test, I get the following:

/test/somefile.txt
rm: missing operand
Try `rm --help' for more information.

so it seems that the $ FILE variable is not being passed to the rm command ... how can I do it right? Thank.

+5
source share
1 answer

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

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

which will wait for the file to be created in / test, specify the file name for xargs and give this argument /bin/rmto delete the file, then it will exit.

( -m inotifywait), script :

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

, / , .

+6

All Articles