Shell Script create script alert file creation

1

I'm trying to create a script, that I detect the creation of directories / files / subdirectories, and that after that, I execute another statement / command

Example: Launch a script, and if in / test / detect that it has been created, create a directory, then enter it within an if

I've tried saving the "size" of a directory, and then making a comparison, but nothing

Here the code

#!/bin/bash

stat -f /var/cache/zoneminder/events/camara_cara/ > /home/arturo/estado2

diff /home/arturo/estado /home/arturo/estado2 > /dev/null

if [ $?  == 0 ]; then
        echo "nada ha cambiado"
else
        echo "se ha añadido ficheros"
#       stat -f /var/cache/zoneminder/events/camara_cara > /estado-actalizable
fi
    
asked by arthur 02.03.2018 в 16:57
source

1 answer

0

Using fairly standard tools for * nix environments, you could do the following:

  • Take a snapshot of the folder

    find ./test/* -xdev -exec md5sum {} \; | sort > before.txt
    

    This does is to search recursively from the folder ./test , files and directories, and try with each of the files to generate a hash with md5sum , then the output is ordered with sort and by last we generate the file before.txt , this file will contain a line for each directory without the hash (because it is a folder) and other lines per file, this time with the corresponding hash

  • Then, simply repeat the process again before comparing:

    find ./test/* -xdev -exec md5sum {} \; | sort > after.txt
    
  • Now yes, compare before.txt and after.txt

    diff -daU 0 before.txt after.txt
    

    If there are no changes we will not get any output, so we could incorporate this in a script as follows:

    DIFF=$(diff -daU 0 before.txt after.txt) 
    if [ "$DIFF" != "" ] 
    then
        echo "Se ha modificado la carpeta"
    fi
    
  • Retrieve and save the hash allows us to evaluate what I was saying, files that have modified their content but not their size. What you have to keep in mind is that to generate this data, it is necessary to read the file completely, so the times will increase depending on the number and size of the files. Also this method does not traquea changes of permissions or attributes.

    If you prefer a simpler form, you could avoid the use of md5sum and simply make a stat -c "%y %s %n" ./test/* | sort > before.txt , with this we retrieve modification date, size and name. The diff will work the same and will alarm against any change in this data.

        
    answered by 02.03.2018 в 18:28