Bash shell script. Notify new files in directory.

Hi

I need a script in bash. Which will inform me about new files in the directory. I am new in scripting that is why cant build script properly.

I think i can do this via "if then"  ow with while.

Case like this.
Somedays in the month it will be uploaded some files to the directory. After adding new files, i want to script notify me that new files added successfully.
amir damirovAsked:
Who is Participating?
 
Kalpesh ChavanSenior Software EngineerCommented:
You can use inotifywait( inotify-tool ) to monitor event for directory.
inotifywait efficiently and waits for changes to files using Linux's inotify  interface.

Pre-requisite: inotify-tool  installed.

Documentation: https://linux.die.net/man/1/inotifywait

Sample Script:
   
DIR=/dir-path/

while FILES=$(inotifywait -e create $DIR --format %f .)
do
   echo == FILES ==
   echo Date: `date`
   echo $FILES
   echo ===========
done

Open in new window


Output:
Setting up watches.
Watches established.
== FILES ==
Date: Fri May 4 11:20:33 UTC 2018
testfiletouch
===========
Setting up watches.
Watches established.

Open in new window


inotifywait  also support diffrent kind of even like create, modify, delete.  you can  specify these event in commandline as follow.

$ inotifywait -e create,delete,modify,move
0
 
Rajat SehgalFounder Enhance TechnologyCommented:
from email alert you want to notify ?
0
 
amir damirovAuthor Commented:
Hi, Rajat.

Yes, from mail. In script i can use " mail -s" command. But i have problems with buildings such scripts. I just need asisst.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Rajat SehgalFounder Enhance TechnologyCommented:
Use this script

#!/bin/bash
touch oldfiles.txt
ls>newfiles.txt
out="$(diff newfiles.txt oldfiles.txt)"
if [ -z "$out" ]
then
      echo "file not change"
else
      echo "${out}"
fi
rm -f oldfiles.txt
cp newfiles.txt oldfiles.txt
0
 
arnoldCommented:
Rajat's using an interesting method comparing list of files.
But does not account for new files created in subdirectories.

Combining the above through the use of oldffiles.txt as a trigger for -newer option in find.
find . -newer oldffiles.txt will list all files added/modified since .....

To only search for new, the limited set of newer would then need to be compared one by one to make sure it is not in the prior listing.
0
 
nociSoftware EngineerCommented:
On linux there is a tool called: incrond (inotification cron) that tool can trigger on changes in a filesystem. f.e. when a file is added to a directory etc.
it will probably need installation first.   see http://incron.aiken.cz/
0
 
Prabhin MPEngineer-TechOPSCommented:
You can setup OSSEC which gives you notification once new files are added. Moreover its HIDS tool as well
0
 
amir damirovAuthor Commented:
Thank you for helping
0
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.