Skip to main content

Command Palette

Search for a command to run...

Linux command 'chmod' in depth

Updated
3 min read
Linux command 'chmod' in depth
K

I'm currently working as Freelancer/Remote developer and crafting Flutter apps with clean code and architecture. I'm learning all about DevOps & Open Source.

Linux is a powerful and versatile operating system that is used by many different types of computers, including servers, supercomputers, and personal computers. One of the key features of Linux is its support for file permissions, which allow the owner of a file or directory to control who can read, write, or execute it.

In this blog post, we will take a closer look at the chmod command, which is used to change file permissions in Linux. We will explain how chmod works, how to use it to set permissions, and some common pitfalls to avoid.

The chmod command is used to change the permissions for a file or directory in Linux. It takes two arguments: the permissions you want to set, and the file or directory you want to change. The permissions are specified using a combination of letters and symbols, as shown in the table below:

PermissionSymbol
Readr
Writew
Executex

The chmod command also uses the following symbols to specify the user or group that the permissions apply to:

User/GroupSymbol
Useru
Groupg
Othero

To use chmod, you combine these symbols to create a permission string, which tells chmod what permissions to set for what user or group. For example, to give the owner of a file read, write, and execute permissions, you could use a command like this:

chmod u+rwx example.txt

This command adds the rwx permissions for the user to the existing permissions for the file. You can also use the - symbol to remove permissions, or the = symbol to set the permissions exactly. For example, to give the owner read and write permissions, and remove all permissions for the group and other users, you could use a command like this:

chmod u+rw,g-rwx,o-rwx example.txt

You can also use numeric values to specify permissions with chmod. Each permission has a corresponding numeric value, as shown in the table below:

PermissionNumeric Value
Read4
Write2
Execute1

To use numeric values with chmod, you add the values for the permissions you want to set. For example, to give the owner read and write permissions, you would use a numeric value of 4 + 2 = 6. To give the owner read, write, and execute permissions, you would use a numeric value of 4 + 2 + 1 = 7.

To use numeric values with chmod, you specify the user or group and the numeric value for the permissions. For example, you could use a command like this:

chmod 777 example.txt

You can also use the chmod command to change the permissions for multiple files or directories at once. To do this, you use the -R option, which tells chmod to recursively change the permissions for the specified files and directories, as well as all of the files and directories inside of them. For example, to give the owner read, write, and execute permissions for all.