Monitoring a Directory for New Sub Directories

89 Views Asked by At

There is a large shared directory on a Linux machine that contains a number of projects. Once per day I need to determine if any new directories have been created since the previous day.

What would be the best way to go about doing this?

Does anyone have any suggestions? I am unable to install additional tools and would prefer a Bash script or something in Perl. Ideally, I would be able to access the file/directory creation date but it seems that only the date last modified is recorded.

I am trying to do something like this, but I can't seem to massage this to work properly. There must be a simple solution.

#!/bin/bash
cd /home/Project/working

if [ ! -e /tmp/new_games_diff_0 ]
then
    echo "First run... cannot determine if there any new games"
    ls -1 > /tmp/new_games_diff_0
    exit
fi

ls -1 > /tmp/new_games_diff_1
gvimdiff /tmp/new_games_diff_1 /tmp/new_games_diff_0 &

cp /tmp/new_games_diff_1 /tmp/new_games_diff_0
1

There are 1 best solutions below

4
On BEST ANSWER

You are correct - linux doesn't (necessarily) track any specific creation times. So you're going to need to compare 'before' and 'after.

Perl has a nice mechanism for this in the form of hashes, and the built in Storable module.

#!/usr/bin/perl

use strict;
use warnings;

use Storable;

my $path    = "/home/Project/working";
my $seen_db = "~/.seen_files";

my %seen;
if ( -f $seen_db ) { %seen = %{ retrieve $seen_db } }

foreach my $entry ( glob("$path/*") ) {
    if ( -d $entry ) {
        print "New entry: $entry\n" unless $seen{$entry}++;
    }
}

store( \%seen, $seen_db );

If you want to store some file metadata in your db - like mtime then it'll be worth looking at the stat function.

However this might be a little overkill - as you might simply find that find will do the trick:

find /home/Project/working -mtime -1 -type d -maxdepth 1 -ls

(You can also use -exec as find option to perform an action on each of the files, like running a script).