Chmod +X

Chmod +X

[#the-short-answer]The Short Answer[#the-short-answer]

On Linux and Unix-like operating systems (MacOS included), the [.inline-code]chmod[.inline-code] command is used to change the permissions of files and directories. The [.inline-code]x[.inline-code] option specifically sets the execute permission on a file, allowing it to be run as a program.

For example, to make a script executable by every user on the system, you can use the following command:

 $ chmod +x script.sh

Then run the script directly:

 $ ./script.sh

Note that the [.inline-code]+[.inline-code] sign here translates to “add permission”.

[#execute-permission-directories]The Execute Permission For Directories[#execute-permission-directories]

In the case of directories, the execute permission has a slightly different meaning than for files, as when set, it allows users to list their content using the [.inline-code]ls[.inline-code] command, or enter them using the [.inline-code]cd[.inline-code] command.

[#setting-execute-permission]Setting The Execute Permission For Specific Users[#setting-execute-permission]

In some cases, you might want to control which user is allowed to execute a file.

To do so, you can use a combination of the letters:

  • [.inline-code]a[.inline-code] for all; which designates all users on the system.
  • [.inline-code]u[.inline-code] for user; which is the user account that created the file or has been assigned ownership of the file
  • [.inline-code]g[.inline-code] for group; which is a collection of user accounts, such as members of the same team, that have been granted certain permissions on the file.
  • [.inline-code]o[.inline-code] for others; which are all other users who are not the owner or members of the group associated with the file.

[#execute-permission-owner-of-file]Use [.inline-code]u+x[.inline-code] to give execute permission to the owner of the file only[#execute-permission-owner-of-file]

[#execute-permission-owner-and-group]Use [.inline-code]ug+x[.inline-code] to give execute permission to the owner of the file and the group[#execute-permission-owner-and-group]

[#execute-permission-everyone]Use [.inline-code]a+x[.inline-code] to give execute permission to everyone[#execute-permission-everyone]

Note that in this last case, executing [.inline-code]chmod a+x[.inline-code] has the same effect as [.inline-code]chmod +x[.inline-code].

[#execute-permission-conditionally]Setting The Execute Permission Conditionally[#execute-permission-conditionally]

The [.inline-code]X[.inline-code] (uppercase [.inline-code]x[.inline-code]) option allows to conditionally set the execute permission of files and directories.

When used:

  • If the target is a directory, then it sets the execute permission for the owner, the group, and the others, allowing them to list and enter the directory.
  • If the target is a regular file, then it doesn’t set the execute permission, unless it is already enabled on at least one of the owner, the group, or the others.

This option is particularly useful when you want to recursively change the permission of a directory and its subdirectories, without affecting the regular files themselves.

For example, let’s consider the following [.inline-code]tmp[.inline-code] directory:

Running the [.inline-code]chmod -R g+X[.inline-code] command on this directory will set the execute permission for the group on:

  • The [.inline-code]tmp[.inline-code] directory.
  • The [.inline-code]dir_1[.inline-code] subdirectory.
  • The [.inline-code]script_1.sh[.inline-code] file.

But will leave the permissions of the [.inline-code]script.sh[.inline-code] file unchanged.

Note that running [.inline-code]chmod +X[.inline-code] will have the same effect as running [.inline-code]chmod a+X[.inline-code].

[#sudo-chmod]Using the [.inline-code]chmod[.inline-code] command with [.inline-code]sudo[.inline-code][#sudo-chmod]

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 files or directories that you do not have access to as the current user.

For example, you can use the [.inline-code]sudo chmod +x[.inline-code] command on a system file to give permission to all users to execute it.

 $ sudo chmod +x /sbin/reboot

It is important to note that giving the execute permission to all users to a file or directory belonging to the system may lead to security vulnerabilities and have unintended consequences.