How would I use inotifywait to execute a command if a file in a directory is created, deleted or modified?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0














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.



#!/bin/sh
while inotifywait -mre create,delete,modify /home;do
echo "test"
done









share|improve this question























  • 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 at 1:21











  • Just updated my origional post explaining what's going on. Thank you for your quick response.
    – Stephen
    Dec 14 at 1:26










  • OK, so it prints the expected message when you create a file. What is not working?
    – l0b0
    Dec 14 at 1:27










  • It's not printing my "test" message
    – Stephen
    Dec 14 at 1:27







  • 1




    "/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing?
    – l0b0
    Dec 14 at 1:29















0














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.



#!/bin/sh
while inotifywait -mre create,delete,modify /home;do
echo "test"
done









share|improve this question























  • 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 at 1:21











  • Just updated my origional post explaining what's going on. Thank you for your quick response.
    – Stephen
    Dec 14 at 1:26










  • OK, so it prints the expected message when you create a file. What is not working?
    – l0b0
    Dec 14 at 1:27










  • It's not printing my "test" message
    – Stephen
    Dec 14 at 1:27







  • 1




    "/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing?
    – l0b0
    Dec 14 at 1:29













0












0








0







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.



#!/bin/sh
while inotifywait -mre create,delete,modify /home;do
echo "test"
done









share|improve this question















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.



#!/bin/sh
while inotifywait -mre create,delete,modify /home;do
echo "test"
done






shell-script inotify






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 14 at 6:32









Rui F Ribeiro

38.8k1479129




38.8k1479129










asked Dec 14 at 1:18









Stephen

62




62











  • 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 at 1:21











  • Just updated my origional post explaining what's going on. Thank you for your quick response.
    – Stephen
    Dec 14 at 1:26










  • OK, so it prints the expected message when you create a file. What is not working?
    – l0b0
    Dec 14 at 1:27










  • It's not printing my "test" message
    – Stephen
    Dec 14 at 1:27







  • 1




    "/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing?
    – l0b0
    Dec 14 at 1:29
















  • 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 at 1:21











  • Just updated my origional post explaining what's going on. Thank you for your quick response.
    – Stephen
    Dec 14 at 1:26










  • OK, so it prints the expected message when you create a file. What is not working?
    – l0b0
    Dec 14 at 1:27










  • It's not printing my "test" message
    – Stephen
    Dec 14 at 1:27







  • 1




    "/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing?
    – l0b0
    Dec 14 at 1:29















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 at 1:21





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 at 1:21













Just updated my origional post explaining what's going on. Thank you for your quick response.
– Stephen
Dec 14 at 1:26




Just updated my origional post explaining what's going on. Thank you for your quick response.
– Stephen
Dec 14 at 1:26












OK, so it prints the expected message when you create a file. What is not working?
– l0b0
Dec 14 at 1:27




OK, so it prints the expected message when you create a file. What is not working?
– l0b0
Dec 14 at 1:27












It's not printing my "test" message
– Stephen
Dec 14 at 1:27





It's not printing my "test" message
– Stephen
Dec 14 at 1:27





1




1




"/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing?
– l0b0
Dec 14 at 1:29




"/home/stephen/ CREATE test" means that "/home/stephen/test" was created. Is that the confusing thing?
– l0b0
Dec 14 at 1:29










1 Answer
1






active

oldest

votes


















0














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.






share|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 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 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 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 at 1:52










  • Nvm just re-read the man page, tyvm
    – Stephen
    Dec 14 at 1:57










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487899%2fhow-would-i-use-inotifywait-to-execute-a-command-if-a-file-in-a-directory-is-cre%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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.






share|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 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 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 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 at 1:52










  • Nvm just re-read the man page, tyvm
    – Stephen
    Dec 14 at 1:57















0














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.






share|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 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 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 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 at 1:52










  • Nvm just re-read the man page, tyvm
    – Stephen
    Dec 14 at 1:57













0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 14 at 1:36









What

938




938











  • 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 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 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 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 at 1:52










  • Nvm just re-read the man page, tyvm
    – Stephen
    Dec 14 at 1:57
















  • 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 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 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 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 at 1:52










  • Nvm just re-read the man page, tyvm
    – Stephen
    Dec 14 at 1:57















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 at 1:39




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 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 at 1:46




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 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 at 1:47




@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 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 at 1:52




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 at 1:52












Nvm just re-read the man page, tyvm
– Stephen
Dec 14 at 1:57




Nvm just re-read the man page, tyvm
– Stephen
Dec 14 at 1:57

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487899%2fhow-would-i-use-inotifywait-to-execute-a-command-if-a-file-in-a-directory-is-cre%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?