Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Syntax Chmod command in Linux?
2.
Chmod Command Options
3.
Modes for Chmod Command in Linux
3.1.
1. Symbolic Mode
3.1.1.
Examples of Symbolic Mode:
3.2.
2. Octal Mode
3.2.1.
Examples of Octal Mode
4.
Frequently Asked Questions
4.1.
What is the use of chmod 777 command in Linux?
4.2.
Is chmod 3 or 4 numbers?
4.3.
What does chmod 755 file?
4.4.
What is 400 in chmod?
5.
Conclusion
Last Updated: Jun 5, 2024
Easy

Chmod command in Linux with Examples

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Have you ever used Linux commands? If yes, you may know that we can also manage the accessibility of a file using a simple chmod command in Linux. The chmod command is an essential tool for managing file and directory permissions. Well, If you don’t have an idea about this command, don’t worry; we have your back!

Chmod command in linux

Let us dive into the world of chmod and gain a better understanding of how to manage file permissions in Linux. Being a tech enthusiast, you know the importance of accessibility to a file system. The chmod command in Linux is used to modify the access mode of a file. Change mode is abbreviated as the chmod command in Linux. This means that every file and directory has a set of permissions to enable the read, write, and execution of the file.

The permissions defined by chmod command in Linux are divided into three categories.

  • ‘r’: The read permission is denoted by the letter r.
     
  • ‘w’: The write permission is denoted by the letter w.
     
  • ‘x’: The execute permission is denoted by the letter x.
     

These letters combine to form a special permission for a specific group of users. Let's have a look at the syntax and options used for the chmod command in Linux.

Syntax Chmod command in Linux?

The syntax used by chmod command in Linux are as follows:

chmod [options] [mode] [File_name]


The parameters used here are kinda self-explanatory, although we are going to see each parameter in detail.

Chmod Command Options

The options used in chmod syntax are the options we have that we can do on the file. It means they are a set of functions provided by the chmod command to help us with the functionality.

Options

Description

-v, --verboseIt will output a diagnostic for every file processed.
--helpIt displays help and exit.
-c, --changesIt is much more verbose but reports only when a change is made.
-R, --recursiveIt applies the permission change to all files and directories recursively.
-c, --reference=RFileIt uses RFile's mode instead of MODE values.
--versionIt outputs the version information and exit.
-f, --silent, --quietIt suppresses most error messages.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Modes for Chmod Command in Linux

Now, the next parameter used in the chmod command in Linux is the modes. When discussing modes of chmod, you should know that they are the permission commands of the chmod commands. They are actually used in setting permissions to new files and directories. 

The modes for chmod command in Linux can be specified in two ways, the symbolic mode and the octal or numeric mode. We’ll discuss both of the modes in detail.

1. Symbolic Mode

Coming to the mode in the chmod command is the symbolic mode. It is the most commonly used approach for establishing file permissions. To set or command what we are going to do with permissions, we must use both letters and operations here.

Below operators can be used in symbolic mode:

OperatorsDefinition
“+”                                                                                It is used for adding permissions.
“-”It is used for removing permissions.
“=”It is used for setting the permissions to specified values.

Below letters that can be used with symbolic mode:

LettersDefinition
“r”                                                    It is used to give read permission.
“w”It is used to give write permission.
“x”It is used to give execute permission.

Below References can be used in symbolic mode:

ReferenceClass
u                                            It is the reference for the owner.
gIt is the reference for the group.
oIt is the reference for others.
aIt is the reference for all(u,g,o).

Examples of Symbolic Mode:

Firstly, let’s check the list of all files that we have in our system. For this, we have created a single file file.txt in our system. You can also check all the text files in your system with this command:

ls -l *.txt
command on terminal

Let’s look at some important examples of symbolic mode in chmod command in Linux:

  1. Adding read, write, and execute permission to the file owner. The letter ‘u’ stands for ‘owner’ of the file. The letter ‘r’ is for read, ‘w’ is for write, and ‘x’ is for execute. The + operator is used to add permission.
chmod u+rwx [file_name]
command on terminal

The file permission after running the above command will look like this:

File running permission

2. Removing the written permission for the group or others. The letter ‘g’ stands for group, whereas the letter ‘o’ and ‘w’ stands for other and group, respectively. The ‘-’ operator is used to remove permission.

chmod go-w [file_name]
command on terminal

The file permission after running the above command will look like:

Above command file permission

3. Adding the read-and-write permission for the owner and read-only permission for the group and others. Here the letter ‘u’ stands for the owner, and ‘+’ adds the permission to read and write. For the next term, we are adding read permission for the group and others.

chmod u+rw,go+r [file_name]
command on terminal

The file permission after running the above command will look like this:

file permission running command

2. Octal Mode

Coming to the first mode is the octal or numeric mode. It can also be used to specify permissions. We specify permission using a three-digit number in this mode. All the digit specifies a different set of permissions:

  • The first digit specifies the Owner's permission.
     
  • The second digit specifies the Group permission.
     
  • The permission for Others is specified by the third digit.
ValuePermission
First Digit                                     It shows the owner's permission.
Second DigitIt shows the group's permission.
Third DigitIt shows others permission.

Examples of Octal Mode

Here are some examples of using the octal mode with the chmod command:

  1. chmod 777 file.txt
    The chmod 777 command gives all permissions to everyone for the file.
     
  2. chmod 644 folder/
    The command 644 gives the owner read and write permissions. Also, gives read-only permissions to everyone else for the folder.
     
  3. chmod 755 script.sh
    The chmod 755 command gives the owner full permissions. It gives read and executes permissions to everyone else for the script.

Frequently Asked Questions

What is the use of chmod 777 command in Linux?

The chmod 777 command in Linux is used to give all users (owner, group, and others) full permissions (read, write, and execute) to a file or directory.

Is chmod 3 or 4 numbers?

`chmod` can use both three or four numbers. 3-number modes set permissions for the user, group, and others. 4-number modes add a leading digit for special permissions like the setuid, setgid, or sticky bits.

What does chmod 755 file?

chmod 755 file means granting the file's owner full permissions (read, write, and execute), and read and execute permissions to the group and others.

What is 400 in chmod?

In `chmod`, 400 sets file permissions so that the owner has read-only access, while the group and others have no permissions. It's represented as `r--------`, meaning only the owner can read the file.

Conclusion

This article briefly discussed the chmod Command in Linux. We have discussed the accessibility of files using the chmod command. We also covered its syntax and modes available in chmod command. You can check out our other blogs to enhance your knowledge:

You can refer to our guided paths on the Coding Ninjas Studio platform. You can learn more about DBMSPythonJavaJavaScript, etc.

Previous article
Tee Command in Linux
Next article
 chown command in Linux
Live masterclass