Tuesday, August 19, 2008

Setting the inital file permissions using umask

umask command is used to set the default permission modes for newly created files in the current shell and its child processes.

umasks are calculated as the AND of the unary complement of the argument (NOT) and the full access mode.

The full access mode is 666 in the case of files, and 777 in the case of directories

A common umask value is 022 (masking out the write permission for the group and others), which ensures that new files are only writable for the owner (i.e. the user who created them). Another common value is 002, which leaves the write permission for the file's group enabled. This can be used for files in shared workspaces, where several users work with the same files.

Example usage of setting umask

Assuming the umask has the value 163, any new file will be created with the permissions 604 and any new directory will have permissions 614 because:

666 AND NOT(174) = 604
666=110 110 110
163 = 001 110 011
NOT(001 110 011) = (110 001 100)
(110 110 110) AND (110 001 100) = (110 000 100)
777 NOT (163) = 604
while

777 AND NOT(163) = 614

777 = 111 111 111
163 = 001 110 011
NOT(001 110 011) = (110 001 100)
(111 111 111) AND (110 001 100) = (110 001 100)
777 NOT (163) = 614

No comments: