Changing file permissions in Unix

So now that we know what file permissions are, how do we change them? Using the command chmod. 

In its simplest form, the command is used in the following manner:

chmod who=permissions filename

The “who” is a list of letters specifying who you’re going to give permissions to. Will it be the user, group, other, or all of them? The permissions are the same letters you see in the directory listing: r, w, x. So consider the example from the previous post:

fileListing2

To change the permissions to allow members of the group staff to modify the file josephus.c would require:

chmod g=rw josephus.c

To remove permissions from others for the directory codePython:

chmod o= codePython

The permissions here are empty, meaning they are all removed. An easier approach is using a shortcut version of chmod. This allows the use of + and in place of the =. So modifying the permissions to allow members of the group staff to modify the file josephus.c would require:

chmod g+w josephus.c

And similarly the directory could be modified in this manner:

chmod o-rw codePython

If you want to be trickier still, there is a shorter shortcut, which uses numbers. This is sometimes known as octal mode. Each permission is given a value, for example read=4, write=2, execute=1. So to change the settings for the user, group and others, to read-write-execute could be achieved in the following manner:

chmod 777 josephus.c

The 777 represents the permissions for the user-group-other: 7 = 4 + 2 + 1. To make a file only read-execute would make the value 4+1=5, So 550 would result in permissions of the form r-xr-x—.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s