What Does “chmod +x ” do and How to Use it?

In Linux, “chmod +x” is a command utility that is used to change the permissions of a file or directory, specifically to make it executable. In this way, users can run it as a program or a script. This “chmod” command makes the file executable of a file or directory, for the file owner, group, or others. This is important for some types of files, such as shell scripts, Python scripts, or binary files, which need to be executed by the system or by other users.

This article will briefly illustrate the “chmod +x” command with possible examples in Linux.

  1. Why Need to Use the “chmod +x” Command in Linux?
  2. What Does the “chmod +x” Command do in Linux?
  3. How to Use the “chmod +x” Command in Linux?
  4. Different File Permissions with chmod+x Command     
  5. What is the Comparison of “chmod 755” and “chmod +x”?
  6. Alternatives of chmod+x Command 

Why Need to Use the “chmod +x” Command in Linux?

In Linux, the “chmod +x” command is particularly useful for making scripts and programs executable. In this way, users can run them without typing their full path or using a dot slash (./) before their name. However, it can also make some files and directories executable that should not be, such as text files, images, or configuration files. 

This can cause security risks or unexpected behavior if someone else tries to run them. Therefore, users should only use this command on files and directories that they trust and that need to be executable.

What Does the “chmod +x” Command do in Linux?

The “chmod +x” command changes the permissions of the file by adding the execute (x) bit to the owner, group, and other users. The execute bit allows the file to be run as a program or a script.

For File Permission

The syntax of the “chmod +x” command for making the file executable is mentioned below:

chmod +x file_name

where “file_name” is the name of the file that you want to make executable. 

How to Use the “chmod +x” Command in Linux?

By default, the “chmod +x” command adds permissions to the file/directory owner. Before adding permission, it is good practice to check the existing permission of the file/directory. For instance, a “script.sh” file is created in the working directory whose permission can be seen via ls command: 

ls -l script.sh

The “script.sh” file does not have execution rights for any user.

Here are some examples of using the “chmod +x” command and permit executable permission in Linux:

Example 1: Make the File Executable For All Users

A shell script named “script.sh” and the user wants to make it executable. For this, execute the “chmod +x” command by using the “sudo” privileges:

sudo chmod +x script.sh

This command sets the execute permission for the owner of the file and allows users to run the script.

Let’s execute the “script.sh” file in the terminal as below:

./script.sh

The output shows the content of the “script.sh” file in the terminal.

Note: To read more about the file or directory permission, check out our guide “Linux File Permissions”.

Example 2: Make Multiple Files Executable

Users can also add execute permissions to multiple files at once by specifying the name of the files. For example, the following command adds execute permissions to both “script.sh” and “code.sh” files:

sudo chmod +x script.sh code.sh

The successful execution of the “script.sh” and “code.sh” files makes them executable.

Different File Permissions With chmod+x Command     

Using the “chmod” command, the execution rights of the file/folder can be modified. The +x option adds the execute permission to a specified target, such as user (u), group (g), or others (o). Different permissions that can be used with “chmod +x” are listed below:

Execute Permission to Everyone

To give execute permission to everyone, use the “a+x” option with the “chmod” command. Here, “a” refers to all (works the same as +x):

chmod a+x code.sh

In this way, executable permission has been granted to all users.

Execute Permission to Current User (Owner)

Let’s take a scenario to permit the current user to execute permission. For this, we specify permission to restrict the execution of the existing file “code.sh” to the current user (owner) only. Here, use the “u” option to represent the (u) user permission:

chmod u+x code.sh

In this way, only the current user has the privilege to execute the “code.sh” script file. 

Execute Permission to Others

The “chmod o+x” will allow anyone to run the file, but not necessarily to read or write it. Here, the “o” represents the other permissions and it is applied via the below command: 

chmod o+x code.sh

In this way, others have granted the executable permission.

Execute Permission to the Group

In this case, the “g” identifies the group permission and is utilized with the “chmod +x” command:

chmod g+x code.sh

It gives the execution permission to the group that can be confirmed from the ls command in Linux.

Execute Permission to the File Owner Along with the Group

Users can also give the execute permission to the owner of the file as well as the group via the “ug+x” option:

chmod ug+x code.sh

It makes the “code.sh” script file executable for the owner and the group. 

Bonus Tip: Make a File Executable from a Directory For All Users

In some cases, make a file executable in the directory. It allows users to access its contents. In our case, specify the file name “Ubuntu/file.txt” to make it executable for all users:

sudo chmod +x Ubuntu/file.txt

The above command sets all users execute permission to the “Ubuntu” directory.

Note: Moreover, the command “chmod a+x” also has the same impact as given in the example.

What is the Comparison of “chmod 755” and “chmod +x”?

The “chmod 755” permits the read, write, as well as execute for the owner, and read along with execute permission for the group as well as others, respectively. The command “chmod +x” allows a file or directory to execute permission for all users. The difference is that “chmod +x” preserves the existing permissions for read and write, while “chmod 755” overwrites them with specific values.

To explore more detail about both these permissions, visit our interesting guide on “ Difference between “chmod 755” and “chmod +x”

Alternatives of chmod+x Command 

Users can also utilize the GUI methods to make files executable in Linux. For this, right-click on the specific file and hit the “Properties” option from the dropdown list. Then, press the “Permission” tab in the menu bar. Click the option “Allow executing file as a program”.

To check the step-by-step instructions, follow our guide on “Make File Executable via GUI”. 

Conclusion

Linux offers the “chmod +x” command to modify the file permissions and make it executable for all users. Moreover, the executable permissions can be limited to a particular user, group, or others. They allow users to run it as a program or a script based on their needs. Additionally, several utilities can also be used to make the file executable based on user, group, or others. This article has briefly explained the “chmod +x” command with all possible examples.