how to automatically delete created file in linux with inotify?

I am trying to delete a created file with inotify but it doesn't work:

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

when i create a file in /test I get this:

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

so it seems that the $FILE variable is not passed to the rm command... how can I do this correctly? Thanks.

Answers


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.


Need Your Help

Regex to change text between tags

javascript regex text replace

I have text that I want to remove and replace. I've have two tags which it lies between:

Android: Perform Small Action When App Leaves Foreground

java android

I am building a timer application and would like to pause it whenever the user removes the app from the foreground (such as via the Back or Home buttons). What would be the easiest way to do this? ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.