tags:

views:

48

answers:

2

Hello,

Lets suppose I have a file with the permission 755 (rwxr-xr-x). I want to chmod g+rw to my file. To end up with 775. Whats the maths that takes place here to calculate that? I cant quite get my head around it thanks.

Or similarly 640, to 660. Whats the formula taking place?

http://articles.slicehost.com/2010/7/17/using-chmod-part-2-octal-mode

Thanks

+1  A: 
U   G   O
rwx r-x r-x ==
111 101 101 ==
  7   5   5

The only math here is the conversion from base-2 to base 10 (or 8, actually).

After adding group r/w, you'll get:

U   G   O
rwx rwx r-x ==
111 111 101 ==
  7   7   5

(U=user G=group O=other)

adamk
Thanks, I understand all that, but if I where writing this in the form or a program how would I go about it?
James moore
A: 

For each of U, G, and O, just replace each r,w, or x with a binary 1 and each - with a binary 0. The result is a binary number. That number cannot be larger than binary 111, which is equal to 7 in both decimal and octal.

So you have one octal number for each of U, G, and O. Now simply put them together in the proper sequence. Technically, you would be multiplying U by 8^2, G by 8^1, and O by 8^0, since octal places are powers of 8.

This all seems to be explained in the link you provided.

EDIT: replying to

Thanks, I understand all that, but if I where writing this in the form or a program how would I go about it?

That would depend on the programming language you are using. Do you want the program to convert the numbers or actually to use chmod?

EDIT: answer for:

Input would be a permissions value eg 660. Along side a string eg. g+rw. The program the calculates the new permission value.

Ok, so first separate the current permissions into U, G, and O, then use the string to tell which of those to modify. You can uniquely find the permissions from the number and vice versa*, and you know that r=4, w=2, and x=1, so whenever you encounter a number which does not have the appropriate condition set, you would add 4, 2, or 1 to it. After that, you should check to see if the result is valid. If not, just set it to 0.

Example: current=660 command=g+rw
u = 6
g = 6
o = 0
from string: we know we are modifying g only
g = 6 means: rw-
since rw is already set, we do not change anything
result = u+g+o = 660
return result

Let's say the command had been a+rw instead. The exact same as above would happen for u and g, but when o is reached, we can see that it is 0, so we add 4 for r and 2 for w, giving us 6. This is a valid number, so we combine them all and return 666.

*Valid Combinations:

rwx 7

rw- 6

r-x 5

r-- 4

scott77777
just convert the numbers i'm using java. Phsudo is fine though.
James moore
@James Can you tell me exactly what you want? What is the input, and what is the output?
scott77777
Input would be a permissions value eg 660. Along side a string eg. g+rw. The program the calculates the new permission value.
James moore