Linux Commands

How to Make a File Executable in Linux

Making bash script files executable is an efficient way to run your bash programs because by default the scripts are not executable. This is useful mainly for system administrators, as they need to develop several bash scripts daily to automate their tasks. For example, you might wish to run bash scripts to back up your work or log certain events on your server. So this article will guide you on how you can make your bash scripts executable, and for that, there are two main ways:

  • Make a bash file executable using the CHMOD command
  • Make a bash file executable using file properties

How to make a file executable using the chmod command

The “chmod” command stands for change mode, and this command can be used to make changes in the file permissions, such as you can make a file writable, readable, and executable. To see these permissions for any file, first, let’s create a bash script file in nano editor:

$ nano bashfile.sh

Then we have written some text and saved the file as shown below:

#!/bin/bash

echo ”This is a test file to make it executable.”

Use the below-mentioned command to check whether the file is executable or not:

$ls -l bashfile.sh

In the above image, the letter ‘r’ shows that the file is readable, and ‘w’ shows that the file is writeable and ‘x’ shows that the file is executable, which is currently missing on the above output, and that means you cannot execute this file for now. To verify this, you can execute this file by following the general syntax shown below.

$ ./file_name

For example:

$ ./bashfile.sh

As you can see, it shows an error after trying to execute the text file, and this is where the chmod command comes in handy. To make this file executable, all you need to do is to follow the general syntax shown below:

$ chmod u+x file_name

For example:

$ chmod u+x bashfile.sh

In the above image, ‘u+x’ shows that you are giving permission to the user to execute a specific file, and you can see that it added a letter ‘x’ in the image and after that, you need to run this bash file by typing.

$./bashfile.sh

It can be seen that the content of the bash file is now executed without any error.

How to make a file executable using file properties

You can also make a file executable by right-clicking on the bash file and selecting its ‘Properties’ as shown below:

The next step is to select the ‘Permissions’ tab and then check the option of ‘Allow executing file as program’.

This will also allow you to execute the bash file for which you have selected this option, upon double clicking the file, you will get the prompt as shown below:

You can either get output in the terminal or directly run the script for the desired result.

Conclusion

Making a bash script executable allows you to execute your bash scripts without having to type bash in the terminal. This is especially useful for system administrators, who need to write several bash scripts every day to automate their jobs. So in this article, we have taught you how you can make any bash file executable, and there are two ways for doing this. One is by using the “chmod” command, and the other is by using the file properties.

About the author

Taimoor Mohsin