This question already has an answer here:

How do I run a script when a file is created in a certain directory?

I am using an IDE to write a program which is run on a different computer. Every time the program is compiled it is copied onto the other computer to /tmp/myFile. The script sets up and runs the program but it I have to run it manually unless it listens for the file creation.

marked as duplicate by jasonwryan, George M, Shadur, Ulrich Dangel, Gilles Feb 9 '13 at 23:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You could use incron (inotify cron).

Same idea as cron, except that instead of being time-based, you define an incrontab that specifies what command to run upon some given event in the filesystem.

The incrontab in your case would look like:

/tmp IN_CLOSE_WRITE /path/to/someCommand $#

And someCommand would be called whenever a file has been written and closed with the filename as argument (and someCommand would need to check if it's been passed a file called myFile).

A note of warning though. /tmp is world writable, so anybody and anything can write files there and can also create symlinks there. So it can cause you to overwrite any file you have write access to when you transfer that myFile (using a symlink), or it can cause you to run another command than the one you expect (like a trojan). It would make more sense to put the file in a directory where only you have write access to.


You can put same script in cron:

while true; do
   modification_time=`stat --printf=%y /tmp/mydir`
   if [ "$modification_time" != "`stat --printf=%y /tmp/mydir`" ]; then

I tested work. You can just change echo line.

Not the answer you're looking for? Browse other questions tagged or ask your own question.