Execution permission to all files created under a specific Directory by default [duplicate]
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.
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
-
You will probably have to install
inotify-tools
first:sudo apt-get install inotify-tools
Copy the script into an empty file, save it as
set_executable.sh
-
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.
- 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.
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