How to Changes the Permissions of a File or Directory
In this tutorial, we will learn how we can change the access permission of a file or a directory in Linux. Before you go ahead with this tutorial, you should first have a basic understanding of File permissions in Linux.
File permissions in Linux can be used to restrict access to any file or directory. We can also provide specific permissions like read-only, read-write, or read-write-execute permission to any user.
We will be using the chmod
command to play around with permissions for files and directories in Linux.
Linux chmod
Command
Whenever we create a new file or directory in Linux, by default, some permissions are configured for that file/directory, which decides who(a user who created the file, any guest user, or others) can access that file or directory and in what mode, like read-only mode, read and write more, or the read, write and execute mode.
Now, what if you want to change the mode of access for any file, to either restrict access to it or to allow free access to it, we can do so by using the chmod
command in Linux.
Syntax
chmod [Options] [Mode] [File]chmod [Options] [Mode] [File]
We will discuss this command with examples and support options.
Linux chmod Options
The following table contains options for the chmod command in Linux.
Option |
Description |
-c, --changes |
It is used to give a diagnosis for all the files that actually changed. |
-f, --silent, --quite |
It is used to suppress most of the error messages. |
-v, --verbose |
It is used to change files and directories recursively. |
-R, --recursive |
It is used to change files and directories recursively. |
--help |
It is used to display a help message and then exit. |
--version |
It is used to give info about the version and then exits. |
Linux Command Modes
Linux supports this command in two different modes:
- Numeric Notation
- Symbolic Notation
Numeric Notation:
In numeric notation, a three figures octal number (0-7) sequence is used to set permission. Each digit holds its own class. The first digit for the user second digit for the group, and the last one is for others. If digits are out of range, then they will be considered zeroes.
7 |
Read, write and execute. |
6 |
Read and write. |
5 |
Read and execute. |
4 |
Read-only. |
3 |
Write and execute. |
2 |
write-only. |
1 |
Execute only. |
0 |
None. |
Symbolic Notation:
The symbolic notation is a combination of letters that specifies permission. Some important letters are (u) for the user (g) for the group (o) for others, and (a) for all the users.
rwx |
Read, write and execute. |
rw- |
Read and write. |
r-x |
Read and execute. |
r-- |
Read-only. |
-wx |
Write and execute. |
-w- |
write-only. |
--x |
Execute only. |
--- |
None. |
Examples: Linux chmod
Command
Let's see some example commands with both numeric and symbolic notation.
The command below will allow only the owner to read the file.
After executing a command, any one of the files will be a lock for editing.
Example: Permission for group users
The below command will give permission to the user as well as a group to read the file.
$ chmod 220 hello.txt
Example: Read Write and Execute Permission
The below command will give permission to everyone to read, write, and execute the file.
$ chmod 777 hello.txt
Conclusion
In this tutorial, we covered how to changes the permissions of a file or directory using the chmod (change mode) command in the Linux operating system with available options and suitable examples.