Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  132] [ 0]  / answers: 1 / hits: 22514  / 3 Years ago, tue, june 22, 2021, 5:54:56

I was asked in an interview why it is that 777 is assigned to take all permissions for a file. Why not 555? He said that there is reason for everything. So, what is the reason for 777? Why not any other number? Is there any significance in this number?


More From » permissions

 Answers
1

I'll try to address the underlying reason why it is 777, rather than aaa, or 999.



Remember that permissions come in the following format:



 u   g   o
rwx rwx rwx


where u=user, g=group, o=other.



Now, imagine you are representing each of these groups as binary. 1 is true, 0 is false.



If you want to give full access to everyone, you would assign the following permissions in binary:



 u   g   o
rwx rwx rwx
111 111 111


Now, if you know binary, you will realise that when you convert 111 from binary to decimal, you get 7.



Thus, you can represent full access as 777.



Note: We are really converting from binary to octal. See the edit below.



This works for all the other access modes as well.



For instance, we can easily work out what 555 means by converting each 5 to binary, and writing it in the above format. 5 in binary is 101, so we have the following permissions:



 u   g   o
r-x r-x r-x
101 101 101
5 5 5


Similarly, if we want to give all permissions to the user, but only allow other people to read, we can find a number representation.



 u   g   o
rwx r-- r--
111 100 100
7 4 4


Now, we know that 111 in binary is 7 in decimal, and 100 in binary is 4 in decimal. Thus, the permissions will be 744.



Edit:



Technically, as highlighted by @LưuVĩnhPhúc and @Braiam, we are converting from binary to octal, as described below. However, the decimal and octal representations of numbers < 8 are the same, so for binary numbers with 3 digits or less, both decimal and octal representations are the same.



When represented as octal numbers, rather than splitting into groups of three, and doing binary to decimal conversion on each group, you can actually take all three groups together as a single binary number, and convert to octal.



For example, here are some binary to octal conversions:



0b111111111 == 0o777
0b101101101 == 0o555
0b111100100 == 0o744


Note that I am prepending "0b" and "0o" to distinguish between binary and octal numbers.



If you want to play around with this, open a terminal, run python and then play around with the following commands:



oct(0b111111111)
bin(0o555)


Remember to prepend "0b" or "0o" to the numbers to let the computer know what base you are interested in. (If you don't, it will assume base 10.)


[#24965] Wednesday, June 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
etzelmortg

Total Points: 430
Total Questions: 105
Total Answers: 106

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;