How to change file or directory permission in Linux/Unix?


We know that the Linux/Unix is a multiuser operating system files and directories are associated with permission so that only authorized users can access the files.

The chmod command is used to change the access permission of files or directories.

Syntax

The general syntax of the chmod command is as follows −

chmod [OPTION]... [Mode]... [File]...

Syntax of chmod command is as followed, it contains some three parameters that will help to set or change the permission of the file.

We will discuss each parameter in detail so that you can have a better idea of using the chmod command.

A brief description of options available in the chmod command −

Sr.No.Option & Description
1-c, --changes
Gives a diagnosis for all the files that actually changed.
2-f, --silent, --quite
It suppresses most of the error messages.
3-v, --verbose
Give a diagnosis for all the processed files.
4-R, --recursive
It changes files and directories recursively.
5--help
Displays a help message and then exits.
6--version
It gives info about the version and then exits.

Mode

Mode can be represented in two different ways.

  • Numeric notation
  • Symbolic notation

Numeric notation

In numeric notation, a three figures octal number (0-7) sequence is followed. Each digit holds Its own class. First digit for user Second digit for group and the last one is for others. If digits are out of range then it will be considered as zeros.

Sr.No.Numeric notation & Description
17
Read, write and execute.
26
Read and write.
35
Read and execute.
44
Read-only.
53
Write and execute.
62
Write only.
71
Execute only.
80
None.

Symbolic notation

Symbolic notation is a combination of letters that specifies the permission. Some important letters are (u) for user (g) for group (o) for others and (a) for all the users.

Sr.No.Symbolic notation & Description
1rwx
Read, write and execute.
2rw-
Read and write.
3r-x
Read and execute.
4r--
Read-only.
5-wx
Write and execute.
6-w-
Write only.
7--x
Execute only.
8---
None.

Some arithmetic operators are used for certain permission.

+” Plus, the operator will be used for adding the next permission to the existing one.

“Minus operator for removing.

=” And equal means that it is the only permission is being used.

Changing the permission of a file

We can change the permission of a file and allow only the owner to read the file using the chmod command.

First, we will check permission of a file using the below command.

$ ls -l

Then we will change the permission of a file using the chmod command. We can provide permission numeric mode or symbolic mode.

Numeric notation –

$ chmod 400 file.txt

Or we can use the below command instead of numeric notation.

Symbolic notation –

$ chmod u + r hello.txt

As mentioned in the above tabular form, the option 200 allows the owner only to write into the file.

After executing the below command, only the owner of the file is allowed to modify it.

$ chmod 200 file.txt

To display more information about the chmod command, we use the --help option with the chmod command as shown below.

$ chmod --help

Updated on: 30-Jun-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements