Gorby has asked for the wisdom of the Perl Monks concerning the following question:

Dear Wise Monks,

I'm running several perl scripts on a Unix server and I'm having trouble with file permissions. I want to change the default file permission settings for any file that my scripts will create using the Umask command, but it seems that the effects of Umask isn't permanent. Is there another command that I can use to change default file permissions for any newly created file in a particular directory?

Thanks in advance.

Title edit by tye

Replies are listed 'Best First'.
•Re: Umask
by merlyn (Sage) on Apr 03, 2003 at 15:05 UTC
    The mode for a new file is a combination of the bits specified by the open/create call and the umask bits. Any bit listed in the umask is cleared from the open/create request. So the umask doesn't force a particular ultimate protection settings: it can only modify a request.

    You mentioned "umask command", so I'm guessing you tried something like:

    system "umask 002";
    This won't work, because the umask is a per-process value. You've just changed the umask in a child shell, not in your Perl process, where it would actually matter.

    What you want is the Perl built-in umask operator:

    umask 002;
    That should affect your current process umask.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Umask
by Improv (Pilgrim) on Apr 03, 2003 at 15:02 UTC
    You might be able to, with considerable effort, get your shell to do this for you. However, it's going to be a lot easier to just set the umask in each of your scripts to whatever you desire. Alternatively, if the umask you want for that directory is one you can live with as your default, set it in your shell's dotfiles. As yet another option that's kind of crude, you might create a special account with a default umask set to your choice, have it run the scripts, and use sudo, if you run them interactively. On most flavors of unix (including Linux) though, you're not going to be able to do exactly what you want. For flavors that can do what you want, the capability is often present in the ACL facilities, so do an 'apropos acl' to figure out what manpages to look at. Note also that on Linux, ACLs are probably going to be showing up in mainstream distributions within the next year or so.
Re: Umask
by Lhamo Latso (Scribe) on Apr 03, 2003 at 16:07 UTC

    I usually setup my umask value in my login rc. For KSH, that is in "$HOME/.profile". For CSH, "$HOME/.login". For Bash, "$HOME/.bash_profile". That works well because your login shell will be the parent of all the sub-processes you create, and receive a copy of that environment.

    "umask 022" or "umask 0022" is the default when not specified. This equates to "chmod 644".

    # umask 0022 # touch test # ls -l test -rw-r--r-- 1 root root 0 Apr 3 10:04 test

    I normally set it to "umask 002", allowing members of my group to have write access to anything I create, resulting in "chmod 664".

    # umask 002 # umask 0002 # touch test # ls -l test -rw-rw-r-- 1 root root 0 Apr 3 10:06 test

    Verify what is currently active with "umask" and no arguments.