Link to home
Start Free TrialLog in
Avatar of eeolivier
eeolivier

asked on

script to check if new file in folder

I urgently need a small shell script that :

- can be run under cron every minute
- check one folder and launch another script if one file has been ADD, RENAMED or DELETED from this folder.
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Hi,

Say the folder is /home/test.
First, generate a log file by "ls -l"
# ls -l /home/test > /home/test_golden.log

> check one folder and launch another script if one file has been ADD, RENAMED or DELETED from this folder
Then the script called /home/test.sh, and another script called /home/subscript.sh
---test.sh --
#!/bin/sh

ls -l /home/test > /home/test.log

diff /home/test.log /home/test_golden.log > /home/diff.log

if [ -s /home/diff.log ] then
   /home/subscript.sh
fi
------------

> can be run under cron every minute
Second, do
# crontab -e
* * * * * /home/test.sh > /dev/nul 2>&1

Regards,

Wesly
Avatar of eeolivier
eeolivier

ASKER

didn't you forget to refresh test_golden.log ?
Hi,

> didn't you forget to refresh test_golden.log
It depends on what's your subscript.sh.
And "refresh test_golden.log" should be done in the subscript.sh

Wesly
./check_sync.sh: line 9: syntax error near unexpected token `fi'
./check_sync.sh: line 9: `fi'
what mean "if [ -s /home/diff.log ]"

there is no function before -s
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America 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
> if [ -s /home/diff.log ] then
>   /home/subscript.sh
> fi

if [ -s /home/diff.log ]
then
   /home/subscript.sh
fi

Wesly