Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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?

share|improve this question
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
    
Thanks for your suggestion @JacobVlijm –  Ibrahim May 5 '14 at 6:24

1 Answer 1

up vote 1 down vote accepted

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

#!/bin/bash
dir=BASH
while true
do
    info=($(inotifywait -e close_write "$dir" 2>/dev/null))
    sleep 1s
    [[ -f $dir/${info[2]} ]] && chmod +x "$dir/${info[2]}"
done

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

while true
do
    inotifywait -e close_write BASH
done

For more information, see man inotifywait.

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

share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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