I would like to create a Bash script that will sit and listen to a directory and when a new directory or file is added it will recursively copy each file to another location. For example:
I could set the script to sit and listen to my home dir /home/
Then when I added a new directory test, containing test.txt
The script would copy test/test.txt to, say, /home/user/Desktop/test
I can not install inotifywait. Is this possible?
Thanks
What you could do is have a cronjob that runs every hour, which uses a function like below, of courseyou could use a different time.
find /home/user/ --mmin -60 -exec echo This file changed: {} \;
This will output every file that was modified in the time given, for the example above 60 minutes.
This is a example I came up with. It copies the directories where changes happened, it only copies the root upper most directory so its not extremely efficient.
find /home/name/* -maxdepth 0 -mmin -60 -exec cp -r {} /some/other/dir/ \;
inotifywait? - Luukinotifywait(or something related, such asincrontab) is the obvious solution here - Chris Daviesinotifywait? - Luukif [ $(ls -1A $inPath | wc -l) -gt 0 ] ;then cp -r $inPath $outPath;in a while loop. But it requires you to know how many files in the directory to begin with. - WireInTheGhostwhileloop with nosleepwill keep one of your CPUs 100% spinning all the time. With somesleep, you'd get polling, which in turn is better accomplished with cron (or equivalent). Also, are you only interested in top-level new directories? What about changes inside them? Should a new file in an existing directory trigger a copy? Finally, should files and directories be copied even if they already exist on the destination? Please, edit new information into your question instead of including it in comments. - fra-san