[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Directory Monitoring

Posted on 2004-04-14
Medium Priority
Last Modified: 2010-04-22
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.
Question by:enkrypter
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 10824405
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.


Expert Comment

ID: 10824571
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.
LVL 12

Accepted Solution

stefan73 earned 1000 total points
ID: 10824587
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";

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

LVL 12

Expert Comment

ID: 10824599
Hmmm, I guess the two time comparisons should be
if($now - $mtime <= $interval)
if($now - $mtime < $interval)
LVL 12

Expert Comment

ID: 10824735
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.


Expert Comment

ID: 10831031
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?


Expert Comment

ID: 10832607
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  xy@y.com newfiles

chown "newupload" -> "notedupload"

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

LVL 12

Expert Comment

ID: 10833155
> 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 :-)))


Author Comment

ID: 10839010

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!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question