I am not sure weather it's possible or not. I am looking for a script/command that will execute a command or run a scripts when I will add a file in a folder.

For instance:

I am practicing bash scripting. After writing a bash script, I have to make my script file to a executable using this command chmod +x filename.sh . All my scripts has stored in a folder named BASH . So, when I add a new .sh file in that folder, I want a command/script to run which will make my .sh file executable.

How to do it?

  • 1
    You could do it with a cron job, checking for new files every minute, but then there could be a delay of (max) a minute. – Jacob Vlijm May 5 '14 at 6:22

You can use inotifywait. To apply chmod +x to every file written in directory BASH, keep the following script running:

inotifywait -m "$dir" -e close_write --format '%w%f' |
    while IFS=' ' read -r fname
        [ -f "$fname" ] && chmod +x "$fname"

If you are curious, you can see all that inotifywait can tell about what is happening in directory bash by running:

inotifywait -m BASH

For more information, see man inotifywait.

To use inotifywait, you may first need to install inotify-tools: run apt-get install inotify-tools.

| improve this answer | |
  • Thanks @John , How can make inotify script running. I have already created the script in my working folder. Do i need to run it using ./inotifyscript.sh this way? – Ibrahim May 5 '14 at 7:55
  • Yes. You can keep it running in one shell/terminal while you create files in other shells/terminals. When you get the script adjusted to your satisfaction, you can run it in the background. – John1024 May 5 '14 at 8:02
  • Yes, I got it and working great! Thanks again @John – Ibrahim May 5 '14 at 8:03

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.