chmod Linux Command

Henrique Deodato
4 min readFeb 17, 2023

--

chmod linux command
Photo by Gabriel Heinzer on Unsplash

Introduction

chmod stands for "change mode" and is a command used in Unix-like operating systems to change the permissions of files and directories. It is a powerful and flexible command that allows users to control the level of access to their files and directories, determining who can read, write, or execute them. In this tutorial, we will discuss the basics of chmod and how to use it to change file permissions.

File Permissions in Unix-Like Operating Systems

Before we dive into chmod, we need to understand how file permissions work in Unix-like operating systems. Each file in the file system has three sets of permissions: owner, group, and others. The owner of a file is the user who created it, the group is a collection of users, and others refer to anyone who is not the owner or a member of the group.

There are three basic types of permissions that can be assigned to each of these sets: read, write, and execute. The read permission allows users to view the contents of a file or directory, the write permission allows them to modify or delete it, and the execute permission allows them to execute it if it is a script or program. The permissions are represented by a series of letters and numbers, as we will see shortly.

Understanding chmod Command Syntax

The chmod command follows a specific syntax. Here is the basic format:

chmod [options] mode file(s)

The options are optional arguments that modify the behavior of the chmod command. The mode is the desired permission setting, and the file(s) are the files or directories to which the permission should be applied.

There are several ways to specify the mode, but the most common one is using a three-digit octal code. Each digit in the code represents one of the three sets of permissions: owner, group, and others. The digits are calculated by adding up the values of the desired permissions: read=4, write=2, execute=1. For example, if we want to give the owner read and write permissions, we add 4+2=6. If we want to give everyone full access, we add 4+2+1=7. We then combine the three digits to form a three-digit octal code. For example, 640 would give the owner read and write permissions, and the group read permissions, while others have no permissions.

Let’s look at a few examples to see how this works:

chmod 644 file.txt

This command sets the file permissions of file.txt to read and write for the owner and read-only for the group and others.

chmod 755 script.sh

This command sets the file permissions of script.sh to read, write, and execute for the owner, and read and execute for the group and others.

chmod 600 secret.txt

This command sets the file permissions of secret.txt to read and write for the owner and no permissions for the group and others.

In addition to using octal codes, chmod also allows us to specify the mode using symbolic notation. This is a more flexible way to set permissions since it allows us to add or remove individual permissions without changing the entire mode. Here are the symbols used in symbolic notation:

  • u for the owner
  • g for the group
  • o for others
  • a for all (owner, group, and others)
  • + to add permissions
  • - to remove permissions
  • = to set permissions explicitly

Here are a few examples of using symbolic notation:

chmod u+x script.sh

This command adds the execute permission to the owner of script.sh.

chmod g-w file.txt

This command removes the write permission from the group of file.txt.

chmod a=rw file.txt

This command sets the file permissions of file.txt to read and write for all users.

Using chmod with Directories

In addition to files, we can also use chmod to change the permissions of directories. When we set permissions on a directory, we are controlling who can access the contents of that directory, but not the contents themselves. Here is an example:

chmod 755 directory

This command sets the permissions of directory to read, write, and execute for the owner, and read and execute for the group and others. This allows users to enter the directory and list its contents, but not modify them unless they have write permission on the individual files or directories within.

Using chmod with Recursive Option

One of the most powerful features of chmod is its ability to apply the same permissions to all files and directories within a given directory. We can use the -R option to apply the permissions recursively. Here is an example:

chmod -R 644 directory

This command sets the file permissions of all files and directories within directory to read and write for the owner and read-only for the group and others.

Conclusion

In this tutorial, we have covered the basics of the chmod command, including file permissions in Unix-like operating systems, the chmod command syntax, and several examples of using the command to change file and directory permissions. chmod is a powerful tool that allows users to control who can access their files and directories. Understanding how to use it effectively is an essential skill for anyone who works with Unix-like operating systems.

Powered ChatGPT, a language model trained by OpenAI

--

--

Henrique Deodato

Geek, Gamer and Founder of Apps&Etc | São Paulo | Brasil