You may not share my excitement at this, but if you are someone for whom CLI, UNIX and/or binary have been some sort of enigma at any point, then you may at least have some sympathy for me.
In the previous post I went through how I made file executable for the first time, by using chmod
command. Taking one step at a time, I did it the boring way as it didn't require additional explanation... Now, I want to look at the more fun version of it which uses binary!
This is the command I looked at last time:
chmod a+x bin/mastermind
If mt current permissions are:
-rw-r--r-- 1 Jarkyn staff 312 12 Mar 15:30 mastermind
Running the above chmod command would change them to the following:
👻 mastermind [master] ⚡ ls -l bin
-rwxr-xr-x 1 Jarkyn staff 312 12 Mar 15:40 mastermind
Which means that we added ability to execute mastermind file to all user types. Lets rewind this change by running the opposite of "+" command:
chmod a-x bin/mastermind
This would take away ability to execute from all the user types and we are back to:
-rw-r--r-- 1 Jarkyn staff 312 12 Mar 15:50 mastermind
Next, we will put ability to execute back fot all user types, but this time instead of cmod a+x
we will use:
chmod 755 <file_path>
Running it would bring us back to
-rwxr-xr-x 1 Jarkyn staff 312 12 Mar 16:02 mastermind
So this code 755 seems to be doing the magic, lets see how it's doing it. First, let's look at those permissions:
rwx r-x r-x
We can think of those as switches for a specific permission in each group:
on on on on off on on off on
And taking it step further we can represent this as binary switches: 1 means on, 0 means off.
111 101 101
Now, if we convert each one to the decimal:
111 => 7 101 => 5 101 => 5
And this is why we can say:
chmod 755 <file_path>
As you imagine there will be a number of permutations representing whether each r/w/x is on or off
--- => 000 => 0 => NO read write or execute --x => 001 => 1 => Execute (because last bit set to 1) -w- => 010 => 2 => Write (because middle bit set to 1) -wx => 011 => 3 => Write,Execute (because last 2 bits set to 1) r-- => 100 => 4 => Read (because first bit set to 1) r-x => 101 => 5 => Read,Execute (because first and last bits set to 1) rw- => 110 => 6 => Read,Write (because first and second bits set to 1) rwx => 111 => 7 => Read, Write, Execute (because all bits set to 1)
Say you wanted following permissions for a file:
rwx r-- --x
This is how the command would look like:
chmod 741 <file_path>
That's it, no magic and no more mystery! Which brings me to the end of this geek out session, hope you found it useful!