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

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

2 Answers 2

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:

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

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

while true
    inotifywait -e close_write BASH

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

As another option, a script, run by a cron job that will check your scripts directory every minute. If it finds none-executable files, it will make them executable.

The script hase a few options which you can set in the headsection of the script:

allowed_extensions = ["all"] 

will set all files to executable

allowed_extensions = [".py",".sh"]

will only set files with given language extensions executable

allowed_extensions = ["only_without"]

will only set files without language extension executable

How to use it

Copy the text below in an empty textfile, save it as set_ex.py in your scripts folder. Then add the following line to the crontab file; (open a terminal and type crontab -e) * * * * * cd /path/to/the/sriptsfolder; python3 set_ex.py


import os
import subprocess
allowed_extensions = ["all"] # (options: "all"; "only_without"; list of extensions, between '', separated by a ','
currdir = os.getcwd()

get_list = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
output = get_list.communicate()[0].decode("utf-8").split("\n")[1:-1]

for line in output:
    filedata = [line[:10], line[43:]]
    get_extension = filedata[1].split(".")
    language_extension = "."+get_extension[-1] if len(get_extension) > 1 else "no_extension"
    situation = ([language_extension in allowed_extensions, allowed_extensions[0] == "all"],
          [allowed_extensions[0] == "only_without", language_extension == "no_extension"])
    if situation[0].count(True) != 0 or situation[1].count(True) == 2:
        file_checkup = [(filedata[0].startswith("d")), filedata[0].endswith("-x"),filedata[1].endswith("~")]
        if file_checkup.count(False) == 3:
                subprocess.call(["chmod", "+x", filedata[1]])
            except Exception:
share|improve this answer
Thanks @Jacob Vlijm , your given solution is also useful. –  Ibrahim May 5 '14 at 15:11

Your Answer


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.