0

I understand how umask works, at least a basic level, when dealing with the permissions of an executable file or directory. However, I struggle when it comes to how umask applies its rules to text files.

For example, consider the umask 037. On newly created executables or directories I understand that the permissions would arise from a simple subtraction (777-037 = 740). My question though is then how does the apparent subtraction work for determining the permissions on say a text file who's default permissions would be 666.

To be clear how is the subtraction done (666-037 = 637?) obviously having an executable text file makes no sense, and according to some examples I have carried out I know that that the file permissions in such a case should work out to 640.

How is this subtraction done? How do we arrive at 666-037 = 640?

tux3
  • 7,171
  • 6
  • 39
  • 51
user3277807
  • 63
  • 1
  • 1
  • 11

1 Answers1

1

It's not subtraction. It's masking: Boolean "AND" using the bitwise complement ("NOT") of the umask. So, think of the umask as the bits that should be removed from the mode.

Create mode  0666 (octal)                         = 110110110 (binary)
Mask          037 (octal) = 000011111 (binary)
(Complement of mask)      = 111100000 (binary)    = 111100000 (binary) &
                                                    ---------
Result       0640                                   110100000

See also http://en.wikipedia.org/wiki/Boolean_algebra#Basic_operations

Gil Hamilton
  • 11,973
  • 28
  • 51