Table of Contents
The chmod (short for change mode) command allows you to change permissions on files and folders.
Command: chmod
Synopsis: chmod [OPTION]… [MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
Option | Long option name | Description |
---|---|---|
-c | –changes | Operates like verbose (-v) but report only when a change is made. |
-f | –silent, -quiet | Suppress most error messages. |
-v | –verbose | Outputs a diagnostic for every file processed. |
-R | –recursive | Change all files and directories recursively. |
User Roles and Permissions for Non-Octal Mode
When formatting using the non octal mode, you will need to know the different roles, operations and permissions that can be used.
Category | Operation | Permission |
---|---|---|
u (user) | + (adds a permission) | r (read) |
g (group) | – (removes a permission) | w (write) |
o (other) | = (sets permission equal to) | x (execute) |
a (all) |
Examples using the non-octal mode.
For the following examples, assume the file test.txt starts out with only ‘Read’ permissions (r) for all three groups (user, group, and other). It will appear as the following:
Adding a single permission to a single group
chmod [MODE]… FILE… – This example uses no options, but demonstrates how to add a ‘Write’ permission (w) to the ‘User’ profile (u) on a file named test.txt. Note that the permissions bracket is now -rw-r–r–. You can do this for each permission (r,w,x) and for each user type (u,g,o)
Adding a single permission to All groups
chmod [MODE]… FILE… – Again, no options are used in this example. Assuming the default permissions (read only) from above, we will add ‘Write’ permissions to all three groups at once. Note we use ‘a’ instead of a single group (u,g,o). When we list (ls) the file, you can see that ‘w’ was added to all three group profiles.
Adding multiple permissions at once.
chmod [MODE]… FILE… – Moving ahead with the base command, this example demonstrates how to add multiple permissions with a single command. In this case, we will add a ‘Write’ (w) permission to the ‘User’ (u) group and the ‘Execute’ (x) permission to the ‘Owner’ (o) group. After listing (ls) the file, you can see the User portion of the permissions are set to ‘rw-‘ and the Group section is set to ‘r-x’ while the Other section remained the same with ‘r–‘.
Using the Verbose Option.
chmod [OPTION]… [MODE]… FILE… – This example is the same eample as above, but this time we added the Verbose (-v) option. This displays the changes that were made as the command is run. As you can see, we do not have to use the ‘ls‘ command afterwards in order to see how the permissions ended up.
Examples using the Octal mode.
For the following examples, assume the file test.txt starts out with only ‘Read’ permissions (r) for all three groups (user, group, and other). IN Octal mode, these are represented as 444.
Changing permissions for a single file
chmod OCTAL-MODE FILE… – Here we use the base command without any options. Assuming 444 (r–r–r–) permissions on the test.txt file, we change it to 755 (rwx-r-x-r-x).
Changing permissions on a directory
chmod OCTAL-MODE [DIR]… – This example shows how us changing the permissions on a directory named php.
Changing ALL files and folders to the same permissions
chmod [OPTION]… OCTAL-MODE [DIR]… – Here we demonstrate how to change all files and folders within a specific folder to the same permissions. By doing this recursively (-R), it will continue to go to all subdirectories and change all files and folders there as well, until there are no more to change.