Can I set the umask on a specific directory?

I have a fairly restrictive umask setting (0077). This is fine, except I have one directory in which I'd like a more permissive setting (0002) to all files created anywhere under that directory. Is there a way to set a umask on a specific directory, so all files created beneath it inherit the same permissions?


Solution 1:

Use ACL's

setfacl -d -m mask:002 /your/dir/here/

http://man-wiki.net/index.php/1:setfacl

If wanting to have a default for a login, just add a "umask 002" to your .bashrc (or whatever shell you use). All new dirs will use this if logged in under that env.

Solution 2:

Provide anther solution implemented with shell hooks and direnv. The following solution may be more compatible in case setfacl is not available on your system. (e.g. macOS)

direnv is an environment switcher for the shell. It knows how to hook into bash, zsh, tcsh, fish shell and elvish to load or unload environment variables depending on the current directory.

Use .envrc to export custom umask value for specific dir, and the exported env var will be unloaded when you leave that dir.

# example .envrc file
export UMASK=0022

Define a hook to change the umask value once working dir is changed.

function _umask_hook {
  if [[ -n $UMASK ]]; then
    umask "$UMASK"
  elif [[ $OSTYPE == darwin* ]]; then
    umask 0077
  else
    umask 0022
  fi
}

# To make the code more reliable on detecting the default umask
function _umask_hook {
  # Record the default umask value on the 1st run
  [[ -z $DEFAULT_UMASK ]] && export DEFAULT_UMASK="$(builtin umask)"

  if [[ -n $UMASK ]]; then
    umask "$UMASK"
  else
    umask "$DEFAULT_UMASK"
  fi
}

# zsh hooks
# trigger _umask_hook once working dir is changed
add-zsh-hook chpwd _umask_hook

# bash
# Append `;` if PROMPT_COMMAND is not empty
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND;}_umask_hook"

For the time being, direnv hook initialization for zsh doesn't support chpwd hook. If the pull request GH-514 has not been merged when you see this page. Please comment out eval "$(direnv hook zsh)" and hook direnv on chpwd manually with following code,

if (( $+commands[direnv] )) && ! (( $+functions[_direnv_hook] )); then
  _direnv_hook() {
    eval "$(command "direnv" export zsh)";
  }
  typeset -agU precmd_functions;
  if [[ -z ${precmd_functions[(r)_direnv_hook]} ]]; then
    precmd_functions=( _direnv_hook ${precmd_functions[@]} )
  fi

  typeset -agU chpwd_functions;
  if [[ -z ${chpwd_functions[(r)_direnv_hook]} ]]; then
    chpwd_functions=( _direnv_hook ${chpwd_functions[@]} )
  fi
fi

Source: dynamic-umask-based-on-cwd.md