Terminus
Linux Chmod Command

Linux Chmod Command

The short answer

On Unix-like operating systems, such as Linux and macOS, the [.inline-code]chmod[.inline-code] command is used to change the read, write, and execute permissions of files and directories.

$ chmod <permissions> <file …>

Where:

  • [.inline-code]permissions[.inline-code] is a list of permissions in the octal or symbolic notation.
  • [.inline-code]file …[.inline-code] is a list of paths to the files or directories you want to change the permissions of.

The meaning of permissions for files and directories

Permissions for files and directories have a slightly different meaning.

[#permission-for-files] Permissions for files [#permission-for-files]

The read permission means that the specified user can open and view the contents of a file, but cannot modify or delete it.

The write permission means that the specified user can modify the contents of a file or delete it.

The execute permission means that if the file contains executable code, such as a shell script or a compiled binary, the specified user can run it as a program.

[#permission-for-directories] Permissions for directories [#permission-for-directories]

The read permission means that the specified user can list the contents of a directory, but cannot create, delete, or modify files within it.

The write permission means that the specified user can create, rename, delete, or modify files within the directory.

The execute permission, combined with the read permission, means that the specified user can enter the directory.

[#the-octal-notation] The octal notation [#the-octal-notation]

The octal notation is a numeric representation of file permissions in the form of a 3-digit number, where each digit (from left to right) represents the permissions for the owner, the group, and the others (e.g., [.inline-code]755[.inline-code]).

In this notation, each individual permission is assigned a numeric value where:

  • [.inline-code]4[.inline-code] represents the read permission.
  • [.inline-code]2[.inline-code] represents the write permission.
  • [.inline-code]1[.inline-code] represents the execute permission.
  • [.inline-code]0[.inline-code] represents the absence of permission.

And then added up to create an individual permission set.

For example:

$ chmod 740 script.sh

This command will give:

  • Read, write, and execute permissions (i.e., [.inline-code]4 + 2 + 1[.inline-code]) to the owner of the file.
  • Read permission (i.e. [.inline-code]4[.inline-code]) to the group.
  • No permissions (i.e. [.inline-code]0[.inline-code]) to the others.

[#the-symbolic-notation] The symbolic notation [#the-symbolic-notation]

The symbolic notation is a text representation of file permissions in the form of 3 sets of 3 letters, where each set (from left to right) represents the permissions for the owner, the group, and the others (e.g., [.inline-code]rwxr-xr---[.inline-code]).

In this notation, each individual permission is assigned a letter where:

  • [.inline-code]r[.inline-code] represents the read permission.
  • [.inline-code]w[.inline-code] represents the write permission.
  • [.inline-code]x[.inline-code] represents the execute permission.
  • [.inline-code]-[.inline-code] represents the absence of permission.

And then chained together to create a permission set.

To change the permissions of a file using the symbolic notation, you can use the following syntax:

$ chmod <who><operator><permissions><file …>

Where:

  • [.inline-code]who[.inline-code] represents who you want to change permissions for, where [.inline-code]u[.inline-code] stands for owner, [.inline-code]g[.inline-code] for group, [.inline-code]o[.inline-code] for others, and [.inline-code]a[.inline-code] for all.
  • [.inline-code]operator[.inline-code] indicates the operation to perform, where [.inline-code]+[.inline-code] is used to add permissions, [.inline-code]-[.inline-code] to remove permissions, and [.inline-code]=[.inline-code] to set exact permissions.
  • [.inline-code]permissions[.inline-code] specifies the permissions to add, remove, or set.

For example:

$ chmod u=rwx,g+x,o-wx script.sh

This command will:

  • Set read, write, and execute permissions to the owner of the file.
  • Add execute permission to the group.
  • Remove write and execute permissions to the others.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]add read and write permissions for the owner[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]chmod[.inline-code] command that can then be quickly inserted in your shell by doing CMD+ENTER .

[#chmod-as-the-super-user] Using [.inline-code]chmod[.inline-code]with[.inline-code]sudo[.inline-code] [#chmod-as-the-super-user]

The [.inline-code]sudo[.inline-code] command is used to execute a command as the superuser (or root).

Executing the [.inline-code]chmod[.inline-code] command with [.inline-code]sudo[.inline-code] allows you to modify the permissions of a file or directory that you do not have access to as the current user.

$ sudo chmod <permissions> <file …>

Note that if you don't have access to the [.inline-code]sudo[.inline-code] command on your system, you'll need to contact the system administrator to modify your access to any files you lack permissions for.

[#permissions-on-symbolic-links] Changing file permissions through symbolic links [#permissions-on-symbolic-links]

The [.inline-code]-h[.inline-code] flag tells [.inline-code]chmod[.inline-code] that if the file is a symbolic link, change the permissions on the link itself, rather than the file it points to.

The [.inline-code]-R[.inline-code] flag tells [.inline-code]chmod[.inline-code] to act recursively, which is generally advised against, as covered in how to run chmod recursively.

Note that the [.inline-code]-R[.inline-code] flag can be combined with the [.inline-code]-H[.inline-code] flag to prevent [.inline-code]chmod[.inline-code] from following symlinks or with the [.inline-code]-L[.inline-code] flag to force [.inline-code]chmod[.inline-code] to follow all symlinks.