The Chmod Command

The chmod command is an abbreviation of change mode. It is used to change access permissions to files directories. If we were to ll(short for ls -l) from our home directory, we many see something like this:

drwxr-xr-x  56 shawn users   4096 Apr  1 12:32 .
drwxr-xr-x   4 root  root    4096 Mar  2 12:30 ..
drwxr-xr-x   3 shawn users   4096 Mar 31 09:57 Desktop
drwxr-xr-x   6 shawn users   4096 Mar 30 16:34 Documents
drwxr-xr-x  18 shawn users   4096 Mar 31 23:05 Downloads
drwxr-xr-x   2 shawn users   4096 Jan 27 20:13 Music
drwxr-xr-x   8 shawn users  20480 Mar 29 12:27 Pictures
drwxr-xr-x  17 shawn users   4096 Mar 29 09:19 Projects
drwxr-xr-x   2 shawn users   4096 May 10  2015 Public
drwxr-xr-x  12 shawn users   4096 May 10  2015 .rbenv
-rw-r--r--   1 shawn users      8 Nov 25 16:56 .rspec
drwxr-xr-x   7 shawn users   4096 Nov 19 23:09 Templates
drwxr-xr-x   4 shawn users   4096 Jan 11 18:59 Videos
drwxr-xr-x   4 shawn users   4096 Apr  1 12:32 .vim
-rw-------   1 shawn users  25347 Apr  1 12:32 .viminfo
-rw-r--r--   1 shawn users    138 Feb 16 12:52 .vimrc

The first set of characters drwxr-xr-x are the permissions of that file/folder. The first charater d states that this is a directory, the next three rwx (which stand for Read, Write, eXecute) are permissions for the owner, preceeding three r-x are permissions for the group, and final three r-x are permissions for others (not owner or member of group).

drwxr-xr-x 3 shawn users breakdown:

dir owner-p group-p others-p sub-dir owner name group name
`d` `rwx` `r-x` `r-x` `3` `shawn` `users`

As we initially stated, the first character d informs us this is a directory. The owner-p is the permissons for the owner of the file object, which in our example is shawn and he has Read, Write, and eXecute permissions. Group-p is for group permissions, so anyone in the users group would have those sets of permissions applied to them. The users group has Read and eXecute permissions. And finally is others, which is anyone besides the owner and/or members of the applied group, which they also have Read and eXecute permissions. sub-dir shows us how many directories that this path holds.

Now that we understand how to determine what permissions are applied to a file object, we can change them using the chmod command.

The chmod command has numerical values attached to each permission. Such as execute has a value of 1, and read has a value of 4. You can add these values so that 5 would equate to read and execute permissions.

# Permission rwx
7 read, write and execute rwx
6 read and write rw-
5 read and execute r-x
4 read only r--
3 write and execute -wx
2 write only -w-
1 execute only --x
0 none ---

Using these numerical values, we are able to apply permisions quickly and easily. You have a share.txt file that you want to give everyone read, write, and execute permissions, you cand do this with the chmod command, value for owner 7, value for group 7, and value for others 7, followed by the filename:

chmod 777 share.txt

How about rwx for owner, rx for group, and read for others:

chmod 754 share.txt

You can view that these permissions have taken place using ls -l share.txt command.

While the chmod command offers a handful of options, the only one we will be covering is the recursive -R option. The recursive option allows us to apply the permissions to the current folder and all of its subdirectories.

chmod -R 775 my_folder/

This will apply rwx to owner and group, and rx to others, in for this folder and all of its sub folders/files.