LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting default permissions for new files under a directory (https://www.linuxquestions.org/questions/linux-newbie-8/setting-default-permissions-for-new-files-under-a-directory-4175414483/)

sabresong 07-02-2012 01:21 PM

Setting default permissions for new files under a directory
 
I'm trying to find a way to set default permissions for newly created files in a specific directory.

I have a directory called scripts, in which I write scripts while I teach myself shell scripting and python. I need all these scripts to be executable. Right now I do it at file creation:

Code:

touch newfile && chmod +x newfile && vi newfile
What I'd like to do is set the default permissions so that any new file created under the scripts directory is automatically executable. I've read info on umask, but if I understand it correctly, that would make all new files executable, not just the ones under scripts.

Is there a way to set default permissions for new files under a specific directory?

Kustom42 07-02-2012 01:29 PM

umask is the shell built-in that controls new file perms. Not sure of any other way to make it apply to one directory only.

However, if this is something you really wanted to do you could use this as a learning experience to create a watchdog script via Perl/Python or even bash to watch for newly created files and make them executable. (Might want to be careful the way you write it to make sure it doesn't over-utilize the server.)

Kustom42 07-02-2012 01:34 PM

Or you could alternatively create a bash script that you can use to create files for you.

Such as:

Code:

./createnewscript.sh newscriptfilename.sh
Where ./createnewscript.sh is a bash script that takes arguments and creates the file for you.

sabresong 07-02-2012 07:16 PM

Thanks for the answer, and for the suggestions that I script what I needed. It took a bit of digging, since I'm kind of new to this aspect of Linux, but I was able to write a simple bash script using inotify. I don't yet know enough to know how much of a load it puts on the server, but here's what I came up with:

Code:

while true ; do
    # define an event
    event=`inotifywait -r -e create -q $HOME/scripts`

    # parse the event into DIR, TAGS, FILE
    set -- $event
    event_dir=$1
    event_tags=$2
    event_file=$3

    # set file executable
    chmod +x $event_dir$event_file
done

I put it into a function, added it to .bashrc, and it works perfectly.

Thanks again!


All times are GMT -5. The time now is 10:52 PM.