Everyone knows that there are certain files in Windows that will execute when you just double click them. These are special files that are programs. These typically have the extension “.exe”. Every software on your PC probably has an executable file somewhere that acts as the starting point for the program. Each game, word processor, and even your browser starts up with an exe file. But it’s not just exe files that can execute. There are also “.com” files though these are pretty rare (I haven’t seen one since the DOS days). Batch files of course contain DOS like instructions. And then there are some installers with extensions like “.msi” which initiate installation of programs.
The point is, that executable files in Windows are identified based on their extensions. If you take a regular program exe file and rename it to something else, it won’t execute. But when it comes to Linux, things are very different.
Any File can be Executed in Linux
Unlike Windows, Linux doesn’t have the concept of file extension based executables. Any file can be executable – you just need to have the right permissions. So whether your script has the extension “.sh”, or no extension at all, you can make it executable with a simple command.
I won’t say that either system is better or worse that the other, but the Linux method gives us a bit more flexibility in what we choose to do with a file. But before we see how to make files executable, let’s have a quick look at file permissions.
Explaining File Permissions in Linux
In Linux, we have three different actions that can be taken with a file:
- Read (r)
- Write (w)
- Execute (x)
And three types of users:
- Owners (u)
- Group members (g)
- Everyone else (o)
Each type of user can have one, or all of the permissions in the first list. So if we want to make a file executable, we have to ask “Who should be allowed to execute this file”. And if the answer is “anyone or everyone”, then we can simply assign the “x” permission to all three types of users. We do this with the command called “chmod”.
Using chmod to Make a Linux File Executable
chmod is the standard command for making changes to the permissions of files. We first specify which users we’re referencing, and then we use a plus sign (+) or a minus sign (-) to add or take away permissions. For example, let’s say we want a file to be only executable by the owner. We use the following command:
chmod u+x testscript.sh
This command specifies that the owner of the file (u), should have execute permissions (x) added to the file (+). On the other hand, if we want everyone to be able to execute the file, we would use the following command instead:
chmod uog+x testscript.sh
This means that all users (owner, others, and group members) should have the execute permissions added to the existing set of permissions.
Actually Executing the File
If you’re running a DOS prompt in Windows, you will be able to execute an exe file just by specifying the name provided that it’s in the current directory. However, for security reasons this will not work in Linux. You need to specify the complete path to the executable file. If it’s in the current directory, you can accomplish this with the command:
This is to prevent someone from creating duplicates of existing commands (like “ls” for example), and placing it in the current directory. For safety, Linux makes sure that we know exactly what we’re running. Either that, or we explicitly place the program in the system’s $PATH variable.
As you can see, making a file executable is pretty easy in Linux once you know how. All you need to do is know exactly who should execute a particular file and you’re done!