I want to set the default permissions for newly created directories to have r-x for others and those of newly created files (non-directory files) to have ---.

If I set the umask to 2, directory permissions get r-x but files' get r--.

I couldn't find any way to achieve what I want with umask.

Ultimately, I want others to be able to traverse the directories but not to read the content of the files. I think that is a very reasonable demand but eventually there seems to be no such setting.

  • Wait, you want no access permissions on the files at all? Not even for their owner? – Jonathan Landrum May 27 '14 at 13:01
  • no. I'm talking about for others. I want to set directories to be r-x and files to be --- for others using umask. – Marcus Thornton May 27 '14 at 13:07
  • Why not do something like chmod 600 foo.bar? I know that's not using umask, but it works. – Jonathan Landrum May 27 '14 at 13:10
  • I want it to be the default setting. Using chmod, it only works on the existed files and the new files and directories still be using default setting. You have to use chmod every time once you have new files. – Marcus Thornton May 27 '14 at 13:13
  • What about setting up a separate mask for files? – Jonathan Landrum May 27 '14 at 13:16

If you only create directories with the mkdir command at the shell prompt, you could have:

umask 7
mkdir() (umask 2 && command mkdir "$@")

In your shell customisation file (~/.zshrc for zsh, ~/.bashrc for bash...).

That is set the umask to 7, but redefine mkdir to a function where the real mkdir is called (with the same arguments ("$@")) with a umask of 2. (note that the (...) create a subshell, so the umask 2 is only applied within that function).

  • What does "$@" stand for? I haven't seen this before. I create directories not only at the shell prompt but using java programs. – Marcus Thornton May 28 '14 at 9:22
  • 1
    "$@" provides all the arguments passed to the program. Which would be the directory name(s) passed to the mkdir function in this case. – Chirag Bhatia - chirag64 May 28 '14 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.