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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amir damirovAuthor Commented:
Thank you for helping
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.