Transfered from Linux Config:
What access modes (permissions) are given to new files you create?
The easiest way to find out what permissions are given to new files you create is to create a file and check its permissions using ls -l command:
However, permissions given to a new created file may vary from system to system. The key variable to set permissions to a newly created files and directories is umask. You can check settings of your umask value by executing umask command without any arguments and options.
$ touch new-file
$ ls -l new-file
On most of Linux / Unix systems the command above will return 0002 . To calculate permissions of a new created file in an accordance with umask value 0002 we subtract this value from 666 in a following manner:
Read on for more reliable umask calculation method.
664 = rw-rw-r--
What all of this means?
Firstly, 666 and 002 are octal representations of a file / directory permissions. Permissions rw-rw-r-- are read like:
- rw- = read and write permissions for on owner
- rw- = read and write permissions for a group
- r-- = read only permissions for everyone else
- r ( read ) = 4
- w ( write ) = 2
- x ( execute) = 1
- ( no permissions ) = 0
Therefore, if we for example want to give an owner of the file read and execute permissions, we add 4 + 1 which gives us 5 ( r-x ). Furthermore, the octal representation of no permissions for a group and others will be 00. Put it together and we get 500. Make sure you understand all of the above before continuing !
Now we can go back again to our previous umask calculation:
This umask calculation will only work for some common umask values such us: 0000, 0007, 0027 . However, calculating umask value of 0014 using the same method as example above will give us:
664 = rw-rw-r--
but files created with umask 0014 have a default permissions of -rw-rwów- . What is wrong?
652 = rw-r-x-w
The best and the most reliable method for calculation umask permissions is by use of bitwise NOT and bitwise AND operators. If you need to refresh your memory on what bitwise AND and OR operators are here are couple examples:
Now that we all know how to calculate bitwise AND and NOT we can calculate umask 0014. For all files we start with an octal base value 6-6-6 and umask 0-1-4 converted to binary.
NOT(0) = 1
NOT(1) = 0
NOT(110) = 001
1 AND 0 = 0
1 AND 1 = 1
001 AND 111 = 001
At this point we can perform NOT operation for binary umask value:
666 = 110 110 110
014 = 000 001 100
And as a last step we calculate AND operation of base and umask binary values:
NOT( 000 001 100 ) = 111 110 011
Note that calculating umask permissions for directories is same as calculating umask permissions for files except that we change base value from 666 to 777. Here is an example of calculating umask permissions for a directory where umask value is 0241:
110 110 110 AND
111 110 011
110 110 010 = 662 = rw-rw--w-
Again we start with conversion of base and umask values to binary:
Now we reform NOT operation on umask:
777 = 111 111 111
241 = 010 100 001
and as a last step we do AND operation of base and umask value:
NOT( 010 100 001 ) = 101 011 110
111 111 111 AND
101 011 110
101 011 110 = 536 = r-x-wxrw-