What access modes (permissions) are given to new created files?

Transfered from Linux Config Disqus comments:

Question:

What access modes (permissions) are given to new files you create?

Answer:

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:


$ touch new-file
$ ls -l new-file

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.

umask

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:

666
002
---
664 = rw-rw-r--

Read on for more reliable umask calculation method.

Long answer:

What all of this means?
Firstly, 666 and 002 are octal representations of a file / directory permissions. Permissions rw-rw-r-- are read like:

[ul]
[li]rw- = read and write permissions for on owner [/li][li]rw- = read and write permissions for a group [/li][li]r-- = read only permissions for everyone else [/li][/ul]

where

[ul]
[li]r ( read ) = 4 [/li][li]w ( write ) = 2 [/li][li]x ( execute) = 1 [/li][li]( no permissions ) = 0 [/li][/ul]

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:

666
002
664 = rw-rw-r--

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:

666
014
652 = rw-r-x-w 

but files created with umask 0014 have a default permissions of -rw-rw—w- . What is wrong?

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:


    NOT(0) = 1
    NOT(1) = 0
    NOT(110) = 001
    1 AND 0 = 0
    1 AND 1 = 1
    001 AND 111 = 001

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.

    666 = 110 110 110
    014 = 000 001 100

At this point we can perform NOT operation for binary umask value:


NOT( 000 001 100 ) = 111 110 011

And as a last step we calculate AND operation of base and umask binary values:

110 110 110 AND
111 110 011
-----------
110 110 010 = 662 = rw-rw--w-

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:

Again we start with conversion of base and umask values to binary:

    777 = 111 111 111
    241 = 010 100 001

Now we reform NOT operation on umask:

NOT( 010 100 001 ) = 101 011 110

and as a last step we do AND operation of base and umask value:

111 111 111 AND
101 011 110 
----------
101 011 110 = 536 = r-x-wxrw-

or simply:


777
241
---
536