Directory monitoring for new subdirectories

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

What would be the best way to do this?

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

I'm trying to do something like this, but I can't seem to get it massaged to work fine. There should 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

      

+3


source to share


1 answer


You're right - linux doesn't (necessarily) track any specific creation time. Therefore, you will have to compare "before" and "after".

Perl has a good mechanism for this in the form of hashes and a built-in module Storable

.

#!/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 keep some file metadata in your db-like mtime

, then the stat

function is worth looking at .



However, this might be a little overkill - as you might just find that find

does the trick:

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

      

(You can also use -exec

as a search option to perform an action on each of the files, for example with a script).

+4


source







All Articles