Set/Remove Permissions in Bash
This lesson will be covering the command that assists in setting or removing permissions.
chmod
Definition:
chmod
command is short for “change mode”, is typically used to change the read/write permissions of files and directories. A mode can either be represented symbolically with unique alphabets or by the permutations of three numbers (octal numbers) as we discussed in the previous lesson. We can also add references in the command to indicate the users on which the specified permissions are being applied. Given below is the syntax to chmod
command.
Syntax:
chmod [references] [options] [mode] [file_name]
Operators
Operator | Meaning |
---|---|
+ | Adds the specified permissions on the files |
- | Removes the specified perfmissions on the files |
= | Add ONLY specified permissions on the file and remove the remaining permissions/modes |
References
References are basically used to indicate the users on which the permissions are applied.
Reference | Meaning |
---|---|
u | User who owns the file(s) |
g | Users in the file group |
o | Users which are neither owner of the file nor in the file group |
a | All users. Same as ugo |
Options
Option | Meaning |
---|---|
-f | suppress error messages |
-v | outputs the file being processed |
-c | to output warnings before making any changes |
-R | Means Recursive. This option is used to change directories recursively |
Example:
- You can set permissions by writing combination of three digit numbers where first digit is to set permission for the owner, second digit is to set permissions for the file group members, and third is to set permissions for other users. OR you can explicity write it as:
chmod u=rw,g=wx,o=r my_file.c
The numerical equivalent for this same command where owner can read and write the file, group members and write and execute the file and other users can only read the file:
chmod 634 my_file.c
- To remove the permission of “execute” for a file for everyone:
chmod a-x my_file.txt
- Allow other users to execute and read the file
chmod o+rx my_file.txt
Get hands-on with 1400+ tech skills courses.