Imagine a situation where you wrote an excellent script to automate a tedious task for you. But when you try to execute it in Linux, all you get is a “Permission Denied” error. Fret no more, as in this article, we will discuss two easy methods to make a file executable in Linux and avoid such permission-denied errors.
Prerequisite: Check Current Linux File Permissions
Before you can make a file executable, it is recommended to check the Linux permissions for the file. Use the ls command to check the privileges allowed for the file:
ls -l <file_name>
For example, we used the command below to check the permissions for the file test.sh.
ls -l test.sh
The first nine characters in the output show the permissions on the file. Here, r stands for read and x stands for executable.
How to Make a File Executable Using chmod Command
In Linux, to change a file’s permissions for different users we use the chmod command. For instance, you can use the chmod command to make a file executable for all users or allow only users of a group to read the file.
Make the File Executable for All Users
To make the file executable for all users in your Linux system, use this syntax:
chmod +x <file_name>
OR
chmod a+x <file_name>
For example, to make the file test.sh executable for all users, use this command:
chmod +x test.sh
OR
chmod a+x test.sh
Make the File Executable for the Owner
The owner refers to the user who creates the file in Linux systems. To make the file executable for only the owner of the file on the same Linux system, use the following syntax:
chmod u+x <file_name>
For example, use this command to make the file executable only for the owner:
chmod u+x test.sh
Make the File Executable for a Group of Users
A group refers to a collection of users having a specific purpose. Here’s the method to grant executable access in Linux to a group with the chmod command:
chmod g+x <file_name>
For example, to make a file named test.sh executable, use this command:
chmod g+x test.sh
Make the File Executable for Every Other User
To make a file executable for every other user on a Linux machine, use this command:
chmod o+x <file_name>
For example, use this command to make the file test.sh executable for every other user except owner and group members:
chmod o+x test.sh
How to Make a File Executable in Linux using GUI
Though the command line method works the fastest and without a hassle, the GUI method is more common among beginners. Different Linux distributions come with different “Desktop Environments”, but the process to make a file executable is pretty much the same:
1. Navigate to the file and select the file.
2. Right-click on the file name and select the Properties option. Alternatively, press CTRL + i on the keyboard. This will open a new “File Properties” window.
3. Click on the permissions tab.
4. Check the box at the bottom which says “Allow executing the file as program”.
Note: The GUI method can only make the file executable for all users in Linux. You will need to rely on the command line methods described above if you wish to selective with permissions.