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?

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

x
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 ?
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
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
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!

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

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

From novice to tech pro — start learning today.