How can I set up a different umask for directories then files?
I need dirs with umask 003
and files with umask 117
umask
is global in bash
. One thing you could do is to create a mkdir
wrapper(a script, you give the name to it) that would change the mask after executing it.
#!/bin/bash
umask 0701 ; /path/to/real/mkdir $1 ; umask 0604
This was answered here:
Remember: For directories, the base permissions are (rwxrwxrwx
) 0777
and for files they are 0666
, meaning, you will not achieve execute permissions on file creation inside your shell even if the umask allows. This is clearly done to increase security on new files creation.
Example:
[admin@host test]$ pwd
/home/admin/test
[admin@host test]$ umask
0002
[admin@host test]$ mkdir test
[admin@host test]$ touch test_file
[admin@host test]$ ls -l
total 4
drwxrwxr-x 2 admin admin 4096 Jan 13 14:53 test
-rw-rw-r-- 1 admin admin 0 Jan 13 14:53 test_file
umask
Unix Specification tells nothing about this file permission math specifics. It's up to the shell developers to decide(and OS makers).
Please note that the standard mkdir
command has an -m
option to set the permission bits at creation time:
-m mode
Set the file permission bits of the final created directory to the specified mode. The mode argument can be in any of the formats specified to the
chmod(1)
command. If a symbolic mode is specified, the operation characters+
and-
are interpreted relative to an initial mode ofa=rwx
.
In your case you could set the umask to whatever you need for the permissions of your files, and use the -m
option for the folder creation.
umask
. BTW,003
for directories seems weird: why would you allow other-read, but not other-execute? That will allow listing the directory, but not accessing any of the files in it. – Barmar Jan 13 '17 at 16:39