Directory Monitoring

Greetings all,

I need some sort of linux toy, script (perl or shell) or C code, that can monitor a directory to see when new files arrive.  I need it to be able to send an email to a person or group of persons when the change is detected.  I also need this program to be able to monitor multiple directories and be able to have a different email list for each directory monitored if I so desire.  It doesn't have to be fancy, just so I can send an email to a person saying "You've got new Files", and then possible list the names of the new files in the email, but that is not a priority.  Emailing is though.

I tried to look for tools that can do this, but couldn't find any to fit my needs.  If anyone has suggestions, please suggest!  If something needs to be written in C I would kindly ask you make the source available via GPL.  After all, Linux was founded on it...  :)

I considder myself an intermediate Linux Admin.  I know shell and perl fairly well, but don't claim to know it all.  I am primarily a Slackware user.  I have GUI clutter.  CLI all the way!  My servers don't even have KDE or X installed.  This programming help is going to be used to notify people when files are available for download on an FTP site.  So, it must be flexible to be able to monitor and notify many different directories and people.

Thanks in advance.
Who is Participating?
stefan73Connect With a Mentor Commented:
Try this Perl script:

#!/usr/bin/env perl
use strict;

my @mondirs=qw( one two three );

my $interval=5;

# Main loop
        my $dir;
        my $now = time();

        for $dir (@mondirs){
                my $mtime = (stat($dir))[9] or die "Can't find $dir: $!";

                if($now - $mtime < $interval){
                        print "New files have arrived in '$dir':\n";    
                        opendir (DIR,$dir) or die "Can't open $dir: $!";
                        my @dir_entries=readdir DIR;
                        closedir DIR;
                        my $entry;
                        for $entry(@dir_entries){
                                $mtime=(stat("$dir/$entry"))[9] or die "Can't get file info for '$entry': $!";
                                if($now - $mtime < $interval){
                                        print "$entry\n";

Hi enkrypter,
Check the modification time of the directory. When a new file was added, it is updated. This can be used for inexpensive, regular monitoring of several directories (use stat() in C or Perl).

Once you detect a change, you can then user opendir/readdir/closedir to get all the gory details.

Run a cron, possibly every ten minutes or so, which touches a date stamp file after it completes.

The cron runs a find. Find every file in the desired structure which is less than a day old (find -mtime -1).
Compare this with the datestamp file, eg in ksh):

if [[ $Filename -nt $datestampfile ]]

If the file found is newer that the datestamp file (-nt) then get the ownership from the file and email the user.
Using perl you would probably create a list of files from the find command (I think there are Find modules for perl). You could then generate a hash, the key being the files owner and the value being a list fo files owned by them. Once complete you could email each set of files to the correct owner.

At the end, touch the datestamp file to reset its time. You might have to be careful incase files arrive during execution of the script. May be wiser to set the time on the file to the starting time of the current execution of the script.

Just a suggestion.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Hmmm, I guess the two time comparisons should be
if($now - $mtime <= $interval)
if($now - $mtime < $interval)
Be aware that the file system date stamp granularity of one second is very coarse. In case you have a file change in the very second you do a monitoring loop, you'll get a duplicate alert. But I suppose this is better than having changes slip through unnoticed.

I am curious, is there a way to do this without spinning in a loop (albeit with sleep calls)?
Therefore, is there support for some sort of notification from kernel?

proposel to make a small shell-script

The main idea is to check (a grep) over file-permissions or better file owner, then change owners or permissions and send a mail with the mail command.

So thing like that: (only a draft)

ll **/* | grep "newupload" >newfiles

if newfiles then
  mail newfiles

chown "newupload" -> "notedupload"

... this should lead to a simple shell-script solution.

> notification from kernel?
AFAIK, not with Linux. But I'm pretty sure Solaris has this. I don't know any details, though.

Regardless if this is handled by Kernel or "by hand", the problem of timing granularity remains. Maybe the only workaround is to set back the file time by one second in case of it being on the "boundary" second. But this is evil :-)))

enkrypterAuthor Commented:

I thank you for your suggestions.  I will not have time to try this immediately, but I will and you have all given me a good staring point.  What I had in mind is a lot more elaborate.  I may end up coding it in C.  If I do I will share it.  I will award poinst as soon as I try this out.  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.