4

I would like yo know if there is any method to provide execution permission to all files created under a specific directory by default.

i.e,

If I create a file say foo.sh under a directory fooscripts, it should have execution permission by default. But if I create same file outside fooscripts it should be as usual.

Severus Tux
  • 9,866
  • Inotify is nice to use on a specific directory, not so much the other way around (exclude a specific dir). Is it importan for you that the files are not executable outside the directory? – Jacob Vlijm Jun 04 '16 at 14:59
  • @JacobVlijm no, outside the directory ---> let it be as usual (default permissions) – Severus Tux Jun 04 '16 at 15:36
  • Ah, yes I should learn to read better :) Will post if I get home if not explained yet, or someone posted a brilliant solution in between :). I am on mobile now... – Jacob Vlijm Jun 04 '16 at 15:40
  • Hi Severus, posted my answer. Please mention if you manage. – Jacob Vlijm Jun 04 '16 at 19:54

2 Answers2

5

Using inotifywait

As mentioned, you can use inotify-tools (inotifywait) to watch a specific directory for changes, for example by the script below, and subsequently set new files executable recursively.

The script uses the inotifywait -command, which is triggered by specific events, set by the -e -option. Luckily the command can be used in combination with multiple event types.
Since you want the files inside the directory to be executable, in the script, two events are set:

-e move

which will notice files moved into the directory, and

-e create

which will notice new files created inside the directory.

Furthermore, the options:

-m -r 

are to make the command run indefinitely ("monitor") and recursively in the directory, while:

--format '%w%f'

outputs the directory (path to the file, %w) plus the filename (%f) that caused the event.


More on options of inotifywait can be found here, or, as always, in man inotifywait

The script

#!/bin/sh
# you might want to change the directory below into the targeted directory
DIR="/home/jacob/Bureaublad/test123"
inotifywait -m -r -e move -e create --format '%w%f' "$DIR" | while read f

do
  chmod +x "$f"
done

How to use

  1. You will probably have to install inotify-tools first:

    sudo apt-get install inotify-tools
    
  2. Copy the script into an empty file, save it as set_executable.sh

  3. In the head of the script, set the path to the targeted folder:

    # change the directory below into the targeted directory
    DIR="/home/jacob/Bureaublad/test123"
    

    ...and test-run the script from a terminal.

  4. If all works fine, add the script to Startup Applications: Dash > Startup Applications > Add.

Note

Note that notifywait acts on changes (events). That implies that files that were added or created before the script ran will not be effected. Nor will it re- set the files executable if you manually and deliberately set them not to be executable, while they are inside the targeted directory.

Jacob Vlijm
  • 83,767
1

No. You could use inotify to watch the directory and chmod new files.

Here is how to find out about inotify:

man -k inotify
for i in $( man -k inotify | awk '{ print $1 }' ) ; do 
    man $i 
    read -p "Print?: " ans 
    if [[ "x$ans" = "xy" ]] ; then 
        man -t $i | lpr -J $i 
    fi 
done
# sr is from surfraw, Shell Users Revoultionary Front Rage Against the Web
sr google inotify
waltinator
  • 36,399