Take the 2-minute tour ×

In our linux server, we have a program running in the background which creates files in a certain directory. I want to get a mail when a new file is added to that dir.

I tried using Java, but that got to complex. So I'm looking for some better idea. Is there some program that can do this or a script?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Well, I'll go with overkill (is there such thing?) and suggest the utilities from the inotify-tools package.

More specifically the inotifywait tool:

# inotifywait -m /tmp
Setting up watches.  
Watches established.

Pipe its output through grep and send it to a Bash loop or something. Voila!


Here's a quick & dirty one-liner:

inotifywait -m /tmp 2>/dev/null | grep --line-buffered '/tmp/ CREATE' | while read; do echo update | mail -s "/tmp updated" john@example.com; done
share|improve this answer
Excellent! I'll try that. Hmm... I cant upvote yet, otherwise I would have :) –  Ed Niell Feb 1 '11 at 18:07

You want inotify. You also might want superuser.com ;-)

share|improve this answer

In this answer I list three Ruby libraries that let you watch a directory for changes. A script using one of these libraries and a mail library (like Pony) would be rather simple.

Using my library and Pony a script might be as simple as:

require 'directorywatcher'
require 'pony'

# Only watch every two minutes
my_watcher = Dir::DirectoryWatcher.new( 'uploads', 120 )
my_watcher.on_add = Proc.new do |file_name,info|
    via:         :smtp,
    via_options: { address: 'smtp.mydomain.com', domain:'mydomain.com' },
    from:        "Upload Notifier <noreply@mydomain.com>",
    to:          "admin@mydomain.com",
    subject:     "New File Uploaded!",
    body:        "A new file '#{file_name}' was just uploaded on #{info[:date]}"
my_watcher.start_watching.join # Join the thread
share|improve this answer
+1 Thanks! Can this be left running in the background or do should I use it through cron? –  Ed Niell Feb 1 '11 at 18:13
@EdNiell You can leave it running (with the modification I just added); I've not tested my library or the others I list in any mission-critical application, so you might want to keep an eye on it for a while. I do, however, have Ruby scripts running with the same pattern (spawn a thread and join it) that run in production for months at a time without issue. –  Phrogz Feb 1 '11 at 19:45

Your Answer


