Linux, Mac and Unix File Permissions (Part 2)

As we saw in Part 1 the chmod command allows you to set/change the file permissions using the octal mode which utilises binary mathematics, however if your not much for working with numbers then thankfully the chmod command also using a Symbolic mode which some users might find easier.
As before any given file has three classes that a person will fit into the User (u), the Group (g) and finally the Others (o). The actions a user can perform on a file are also Read (r), Write (w) and Execute (x) as before.
The syntax for chmod in Symbolic mode can be perform in one of two ways. You can either simply add/remove a specific action from a class by using the following command:
chmod u+r /example/file
chmod g-x /example/file
The top example would add Read rights to the User class and the bottom example would remove the Execute rights from the Group class. This approach is useful when you need to simple add/remove rights but leave other access rights intact.
A very common use for this method is to make scripts executable by using the following command:
chmod +x file
As you might have noticed I didn’t specify any of the classes u,g or o however in the event that none have been specified the access right is applied to all three u,g and o and hence is the same as the command below:
chmod ugo+x file
The second approach for Symbolic mode is when you need to implicitly  set the access rights and need to reset any previous rights. Example commands would be as follows:
chmod ugo=rx /example/file
chmod u=rwx,g=rx,o= /example/file
The top example would give User, Group and Others the Read and Execute rights and disable all other rights.
The bottom example would assign full Read, Write and Execute to the User, the Group would get Read and Write and finally the Others would have all their rights reset to none.
The Recursive Option
One last useful option for the chmod command is the ability to apply it recursively to a folder and all of its contents, this can be done with the -R option and can be seen in the following examples:
chmod -R 777 /example/folder
chmod -R ugo=rwx /example/folder
Both examples above will full access to everyone to every single file and folder contained within the specified folder. Take note that the -R option is applied before the Octal or Symbolic options as it is independent of either mode and works exactly same across both modes. This quite useful when you need to make bulk changes within the same folder.
Manipulating User and Group Ownership
Two useful commands when modifying file permissions is the ability to modify who is the User and Group associated with a file/folder.
You can set the User who is the owner of the file by using the chown (change owner) command. The following command will set the User to be a user called steven:
chown steven /example/file
You can also set the Group with the chown with the following command (however this isn’t the normal way of doing it):
chown :staff /example/file
This would assign the group staff as the owning Group of the file/folder. You can also combine both commands into one as follows:
chown steven:staff /example/file
The chgrp (change group) command is the more common way of setting which group as ownership over a file/folder and is used in the following way:
chgrp staff /example/file
This would obviously assign the group named staff ownership of the file/folder.
Both commands also incorporate the ability to set the access rights of either the User and Group classes much like the chmod command however I will not cover this here and more information can be found in their respective man pages.