Skip to main content
Tips

How to Make a File Executable in Linux terminal?

New to Linux command line and wondering how to make a bash script or some other file executable? Here's how to do it.

β€” Team LHB

Warp Terminal

Each file that is in a POSIX-compatible file system (Ext4, Btrfs, XFS, JFS etc) has "mode bits" assigned to itself.

To make a file executable in Linux, the executable mode bit needs to be enabled.

To set the executable mode bit, the chmod command is used like this:

chmod u+x <file>

With that, you can execute said file from the terminal:

./file

That was the quick summary. Let's see things a bit in detail.

Make a file executable in Linux

There are several ways you can make a file executable in Linux. The most common methods involve using the chmod command, in different ways.

To check if you can execute a file, use the -l flag with ls command.

$ ls -l lhb.txt
-rw-r--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

In the first column, -rw-r--r-- represents that the owner can read and write, users from the group can only read and everyone else can only read the file.

Note the lack of x from the permission symbols. This means that lhb.txt is not executable.

I highly recommend reading about Linux file permissions and brushing up your basics to better understand these commands.

Linux File Permissions and Ownership Explained with Examples
Linux file permissions explained in simpler terms. Also learn how to change the file permissions and ownership in Linux in this detailed beginner’s guide.

Method 1: Make file executable for everyone

The first method, and the most straightforward one, is to make a file executable using the following command:

chmod +x <file>

The x flag is to set or unset the executable permission of a file. And using + sign before x means we want to set it as an executable file.

This will make the file executable for the owner, group and everyone else. Anyone on the system, will be able to execute it as well.

$ ls -l lhb.txt
-rw-r--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

$ chmod +x lhb.txt

$ ls -l lhb.txt
-rwxr-xr-x  1 pratham  staff  0 Mar 10 20:49 lhb.txt

As you can see now, everyone, the owner, group and others have the executable x bit set. This means everyone can execute this file now.

Method 2: Make file executable only for certain user or group

If you do not want to make everyone able to execute a file, you should specify classes before adding/removing file permissions.

The available classes are:

  • u : permissions for owner/user
  • g : permissions for group
  • o : permissions for others
  • a : permissions for everyone

When you specify a class, the syntax is mostly similar to the one you saw in the first method.

chmod <class>+x <file>

This means, if I want to make the file executable in a manner such that only the owner of the file can execute it, and no one else, then I should use the following command:

chmod u+x <file>

Here's an example:

$ ls -l lhb.txt
-rw-r--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

$ chmod u+x lhb.txt

$ ls -l lhb.txt
-rwxr--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

Similarly, to make it executable for everyone in the group owning that file, I should use the following command:

chmod g+x <file>

Here's an example:

$ ls -l lhb.txt
-rw-r--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

$ chmod g+x lhb.txt
$ ls -l lhb.txt
-rw-r-xr--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

When you specify the a class, it is almost as if you did not specify any. Let's have a look.

$ ls -l lhb.txt
-rw-r--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

$ chmod +x lhb.txt

$ ls -l lhb.txt
-rwxr-xr-x  1 pratham  staff  0 Mar 10 20:49 lhb.txt

$ chmod 644 lhb.txt #reset permissions

$ chmod a+x lhb.txt

$ ls -l lhb.txt
-rwxr-xr-x  1 pratham  staff  0 Mar 10 20:49 lhb.txt

These commands might appear differently, but they have the same outcome.

Method 3: Use the octal numbers

If you follow the first method, you only enable the executable flag of a file by doing a +x, making it such that anyone can execute it.

But, sometimes you might not want everyone to execute a file, maybe it should need some privileges. In that case, you can specify the full set of permissions.

That is usually done by either providing an octal value or a symbolic value to enable file permissions.

This allows you to have a more granular control over how the flags are set. Below is the syntax for using octal values with chmod command:

chmod <octal> <file>

If you are not good with numbers, you can use an online chmod calculator, which helps you generate correct octal values and thus avoid using the wrong value and messing up file permissions.

Say, I want to make the file executable only for the owner and group, I need to know its current permissions first, and add the executable bits to it on top of it using [chmod-calculator].

Let's see how to do that.

$ ls -l lhb.txt
-rw-r--r--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

$ chmod 754 lhb.txt

$ ls -l lhb.txt
-rwxr-xr--  1 pratham  staff  0 Mar 10 20:49 lhb.txt

As you can see, only the owner and group can execute the file.

Conclusion

This article covers making a file executable in a Linux system. It mainly focused on the different ways to use the chmod command. If you want to know more about it, we have a few examples on how to use the chmod command.

Team LHB