Bash shell script. Notify new files in directory.


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.


Sample Script:

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

Open in new window

Setting up watches.
Watches established.
== FILES ==
Date: Fri May 4 11:20:33 UTC 2018
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
Rajat SehgalFounder Enhance TechnologyCommented:
from email alert you want to notify ?
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.
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

touch oldfiles.txt
out="$(diff newfiles.txt oldfiles.txt)"
if [ -z "$out" ]
      echo "file not change"
      echo "${out}"
rm -f oldfiles.txt
cp newfiles.txt oldfiles.txt
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.
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
Prabhin MPEngineer-TechOPSCommented:
You can setup OSSEC which gives you notification once new files are added. Moreover its HIDS tool as well
amir damirovAuthor Commented:
Thank you for helping
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.