Link to home
Start Free TrialLog in
Avatar of amir damirov
amir damirov

asked on

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.
Avatar of Rajat Sehgal
Rajat Sehgal
Flag of India image

from email alert you want to notify ?
Avatar of amir damirov
amir damirov

ASKER

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.
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
Avatar of arnold
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.
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/
SOLUTION
Avatar of Prabhin MP
Prabhin MP
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for helping