Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
When you want to execute a script or an AppImage, the file must have executable permissions. While it should complex, it is not and requires a single command to make a file executable.
The easiest way to make a file executable is to use the chmod command with the +x
flag as shown here:
chmod +x Filename
Once you make the file executable, it can be executed by appending ./
to the filename as shown here:
./filename.sh
But there are other methods with more options and flexibility and in this tutorial, I will walk you through 4 ways you can make a file executable in Linux:
But before we get started, let’s take a look at some basics of file permissions which will be helpful to understand if the file is executable or not.
Table of Contents
To know if the file is executable or not, you’d have to check the file permissions. To do so, you’d have to use the getfacl command as shown here:
getfacl Filename
For example, if I want to check the file permissions for the Hello.txt
file, then I’d use the getfacl command in the following manner:
getfacl Hello.txt
The getfacl
commands print permissions for user, group and others and as you can see, the user and group have read/write permissions whereas others are left with read-only.
Now, let’s take a look at how to add execution permissions.
When you make a file executable for everyone, all the logged-in users can execute that particular file. And to do so, you use the chmod
command with the +x
flag.
Here, the x
is used to add/remove execution permission to a file and when you append a +
sign, you are instructing a system to add execution permissions. Here’s a syntax:
chmod +x Filename
For example, if I want to make the file Script.sh
executable, then I’d use the following command:
chmod +x Script.sh
As you can see, it added execution permission for everyone.
Making files executable for everyone might not be a good idea, especially if there are multiple users assigned to a single computer. For those times, you can add permissions to a specific group.
To make a file executable for a specific group or a user, you need to use classes while executing the chmod command. Here are the available classes for the chmod command:
u
: permissions for owner/user.g
: permissions for group.o
: permissions for others.a
: permissions for everyone.Here’s a syntax that you need to follow to specify classes while using the chmod command:
chmod <class>+x Filename
For example, if I want to add execution permission for a group, then I will use the following:
chmod g+x Script.sh
As you can see, it added execution permission for the group.
Using octal numbers is more of an advanced way to specify file permissions. Using octal numbers, you can be more selective with what permissions you want to assign to a user, a group or others.
Octal is a number which represents a set of permissions ranging from 0 to 7. Here’s a table for your reference:
Octal Value | Permission | Description |
---|---|---|
0 | — | No permissions |
1 | –x | Execute only |
2 | -w- | Write only |
3 | -wx | Write and execute |
4 | r– | Read only |
5 | r-x | Read and execute |
6 | rw- | Read and write |
7 | rwx | Read, write, and execute |
You are supposed to specify octal for user, group and others in a sequence. This means you will be using a 3-digit number where the first digit represents the user, the second digit is for the group and the third one is for others.
So if I use 754, it means:
Now, let’s take a practical example.
If I want to set permission where the user can read, write and execute whereas the group and others can only read, then I will use the following:
chmod 744 Script.sh
If you are not comfortable using the terminal, then this method will allow you to make the file executable without executing a single command in the terminal.
I find it useful when I want to run an AppImage as soon as possible.
To make a file executable without a terminal, first open your file manager and then switch to a directory where the target file is located.
Now, right-click on the file and choose the Properties
option, go to the Permissions
tab and check the Allow executing file as program
option as shown here:
Once you make a file file executable, you can run it by pressing the enter key but if it is a script, then you have to open a terminal to execute it.
In this tutorial, I went through four ways how you can make a file executable in Linux. Each method has its own advantage. Personally, I use the first method a lot as I don’t share my computer with other users.
Let me know what method you guys would use to make a file executable. Also, if you have a recommendation on what should we cover next, then please leave us a comment.