umask

umask EXPR
umaskThis function sets the umask for the process and returns the
old one using the umask(2) syscall. Your umask
tells the operating system which permission bits to
disallow when creating a new file, including files
that happen to be directories. If EXPR is
omitted, the function merely returns the current umask. For example, to
ensure that the “user” bits are allowed and the “other” bits disallowed,
try something like:
umask((umask() & 077) | 7); # don't change the group bits
Remember that a umask is a number, usually given in octal; it is
not a string of octal digits. See also oct, if all you have is a string. Remember
also that the umask’s bits are complemented compared to ordinary
permissions.
The Unix permission rwxr–x–––
is represented as three sets of three bits, or three octal digits:
0750 (the leading 0 indicates octal
and doesn’t count as one of the three digits). Since the umask’s bits
are flipped, it represents disabled permissions bits. The permission (or
“mode”) values you supply to mkdir or
sysopen are modified by your umask,
so even if you tell sysopen to create
a file with permissions 0777, if your
umask is 0022, the file is created
with permissions 0755. If your
umask were 0027 (group can’t write; others can’t read, write, or execute), then passing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access