Operating System - HP-UX
1828304 Members
3053 Online
109975 Solutions
New Discussion

umask and file permission

 
vinod_25
Valued Contributor

umask and file permission

hi,

we know default file permission is 666 and umask is 022... which absolutely fine... when i create any new file i get 666-022 which is 644 _rw_r_ _ r_ _

but when i set the umask to 033 or 055 then this calculation is not working fine...
that is for umask of 033 the file permission should be 666-033 which is 633 _rw_ _wx_wx but the new file permission still remainst to be _rw_r_ _r_ _, the same is for umask 055

Please help

vinod
7 REPLIES 7
Bill Hassell
Honored Contributor

Re: umask and file permission

umask is applied to the default permissions for files and directories. However, the default permission for a file is always 666, never 777 (which includes the execute bit). The execute is very unusual for a file -- only scripts should have the execute bit set. Setting execute for data files can produce unprecdictable errors.


For directories, the default is 777 since all directories need the execute bit set for searching.


Bill Hassell, sysadmin
Court Campbell
Honored Contributor

Re: umask and file permission

if you set the umask to 033 you are basically doing the same thing as 022. I will try to explain this as best as I can. Here is a translation of rwx:

r = 4
w = 2
x = 1

If files have a default perm of 666 that would be rw-rw-rw-. So a umask of 033 would say remove w and x from the file. Being that the newly created file would not have the execute bit set there is nothing to take away. So you should get rw-r--r--. The only thing being taken away is writes for the group and other. I hope that made sense. If you think about it, it makes sense.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Court Campbell
Honored Contributor

Re: umask and file permission

I should also clarify that 033 would work different for a directory, since it actually has the execute perm set by default. So it would be rwxr--r--.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Heironimus
Honored Contributor

Re: umask and file permission

umask isn't a subtraction, it's a bitmask. The bits in the umask are removed from the original value, since x isn't there you can't remove it. However, if you set your umask to 033 and create a new directory you'll see that it doesn't have execute for group/other anymore.

7 = 0111 (-rwx)
6 = 0110 (-rw-)
5 = 0101 (-r-x)
3 = 0011 (--wx)
2 = 0010 (--w-)
Sajjad Sahir
Honored Contributor

Re: umask and file permission


Dear Vinod

file permission
rwx where r is read =4
w for write =2
x is execute =1
4+2+1=7 this is file permssion

and please read the above threads regarding umask
Dennis Handly
Acclaimed Contributor

Re: umask and file permission

Heironimus: umask isn't a subtraction, it's a bitmask.

Right. It is value & ~umask_value. No need for complicated explanations, let the hardware do the work:
$ typeset -i8 x=0
$ (( x = 8#666 & ~8#22 ))
$ echo $x
8#644
$ (( x = 8#666 & ~8#33 ))
$ echo $x
8#644
$ (( x = 8#777 & ~8#33 ))
$ echo $x
8#744
James R. Ferguson
Acclaimed Contributor

Re: umask and file permission

Hi Vinod:

In keeping with Dennis's suggestion, but using Perl (of course):

# perl -e 'printf "%4o\n",(0666&~022)'
644
# perl -e 'printf "%4o\n",(0666&~027)'
640

Regards!

...JRF...