Introduction to chmod

Before the actual understanding I never really tried how chmod works but I've used it nevertheless for various purposes like running a shell script , changing permissions of an aws ssh key etc.

I didn't even care about the working I just execute this good ol' command on one of my shell script chmod +x I came to know about chmod during one of the lectures on Operating Systems in College which was pretty usefull in a way if anyone would ask what am I doing with chmod while demonstrating something then I'd be able to answer properly

Usually the chmod command is like this


This XXX takes upto three octal digits(0-7) for example 777 ,these digits define permisions for file owner,Group and Others(public) respectively.

Suppose we have to grant Read write permission(not execute) to the user and none for the others then the corresponding command would be

chmod 600 FILENAME
7read, write and executerwx111
6read and writerw-110
5read and executer-x101
4read onlyr--100
3write and execute-wx011
2write only-w-010
1execute only--x001
Note that the first digit 6 from 600 comes from combination of three binary bits which is corresponding to rw-. 6 corresponds to 110 in binary also.If we want to give the user execute permissions this bits would've been 111 which is 7 in octal. In the same way this also works.
chmod a+rw FILENAME

a will mean for all classes (File Owner,Group and others) To remove the permission you can do

chmod a-rw FILENAME

Then there is this -R Flag it comes into action when you are changing mode of a directory and want to apply same permission to every file/directory reciding inside a directory

chmod -R 700 /path/to/directory

Now Let's now make our own shell script and see chmod in action. Create a file named Fill in its content with

echo "First shell script"

then just give the executable permission to our script

chmod +x

Boom your very first shell script is ready, run it with

./ You will now see the output on the command line :)

Kudos on your first ever shell script!. That should be enough for introduction.

At last thank you for reading this and if you have any kind of feedback feel free to hit on this anonymous Feedback link.