chmod command

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…

Options:
OptionLong option nameDescription
-c–changesOperates like verbose (-v) but report only when a change is made.
-f–silent, -quietSuppress most error messages.
-v–verboseOutputs a diagnostic for every file processed.
-R–recursiveChange 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.

CategoryOperationPermission
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:

   # ls -l test.txt   -r--r--r--  1 root    root     29K Jun 17 12:01 test.txt

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)

 # chmod u+w test.txt # ls -l -rw-r--r-- 1 root root 29K Jun 17 12:01 test.txt

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.

 # chmod a+w test.txt # ls -l -rw-rw-rw- 1 root root 29K Jun 17 12:01 test.txt

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–‘.

 # chmod u+w,g+x test.txt # ls -l test.txt -rw-r-xr-- 1 root root 29433 Jun 17 12:01 test.txt

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.

 # chmod -v u+w,g+x test.txt mode of `test.txt' changed to 0654 (rw-r-xr--)

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.

   # ls -l test.txt   -r--r--r--  1 root    root     29K Jun 17 12:01 test.txt

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).

 # chmod 755 test.txt # ls -l test.txt -rwxr-xr-x 1 root root 29433 Jun 17 12:01 test2.txt

Changing permissions on a directory

chmod OCTAL-MODE [DIR]… – This example shows how us changing the permissions on a directory named php.

 # chmod 755 php mode of `php' changed to 0777 (rwxrwxrwx) # ls -l drwxrwxrwx  3 root root 4.0K Jun 15 09:08 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.

 # chmod -R 755 php mode of `php' changed to 0777 (rwxrwxrwx) # ls -l drwxrwxrwx  3 root root 4.0K Jun 15 09:08 subfolder/ -rwxrwxrwx  3 root root 5.0K Jun 15 09:08 test1.txt -rwxrwxrwx  3 root root 2.0K Jun 15 09:08 test2.txt -rwxrwxrwx  3 root root 9.0K Jun 15 09:08 test3.txt -rwxrwxrwx  3 root root 7.0K Jun 15 09:08 test4.txt

Was this article helpful? Join the conversation!