Here is the shell script I've got so far. I want it to check recursively, hence the following options:

  • -r for recursive
  • -m for monitoring
  • -e for event notification and tracking

For a reason unknown to me, this approach isn't working. I'm creating/modifying/deleting files using rm/nano/touch etc and in the terminal I ran the script I get a message saying that a particular operation has been used, where it was used and the file it was used on e.g. /home/stephen/ CREATE test where test is the file I've created using touch.

while inotifywait -mre create,delete,modify /home;do
       echo "test"
  • Welcome to Unix.SE! Can you show exactly how you create, delete or modify files to trigger this? Because "not working" is usually not specific enough for anyone else to debug the issue. Other possible issues include whether you have access to the files and remote filesystems. – l0b0 Dec 14 '18 at 1:21
  • Just updated my origional post explaining what's going on. Thank you for your quick response. – Stephen Dec 14 '18 at 1:26
  • OK, so it prints the expected message when you create a file. What is not working? – l0b0 Dec 14 '18 at 1:27
  • It's not printing my "test" message – Stephen Dec 14 '18 at 1:27
  • 1
    "/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing? – l0b0 Dec 14 '18 at 1:29

I believe inotifywait -m does not exit causing the while-loop not to run as expected.

while inotifywait -r /home -e create,delete,modify; do { echo "test"; }; done however should work as you expect it.

| improve this answer | |
  • hmmm, I've just tested this and seems like you're right. Do you know of a way for this to repeat the echo "test" any time a new modification/deletion or creation is made? – Stephen Dec 14 '18 at 1:39
  • Do you want to omit the message of inotifywait and only echo from inside of the loop? The current command does already output echo. You can always use -q to silence inotifywait. – What Dec 14 '18 at 1:46
  • @Stephen and if you do not want any output from inotifywait, you can use /dev/zero to get rid of the event. while $(inotifywait -q -r . -e create,delete,modify >/dev/null); do { echo "TEST"; }; done – What Dec 14 '18 at 1:47
  • I think I've just found my solution, I did what you suggested but put that in a while true loop and it works as I hoped, Thank you very much. One final question is there a way to omit the /home/stephen/ CREATE (dir/filename), -q removed the "setting up watches" and "watches setup", Just wondering if I can remove this "/home/stephen/ ......" part – Stephen Dec 14 '18 at 1:52
  • Nvm just re-read the man page, tyvm – Stephen Dec 14 '18 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.