make file executable in linux

How To Make File Executable in Linux

In Linux, if you need to run a file or program, it needs to be an executable, that is your Linux user or process needs to have the permission to execute. Sometimes you may need to make file executable in Linux. For example, you may want to make a shell script executable in Linux. By default, when you create a new file on your system and add some code to it, it is unlikely to be an executable. It will have read & write permissions for your Linux user but you won’t be able to execute it right away. In this article, we will learn how to make a file executable. You can use these steps to make any kind of file executable – shell scripts, python or perl scripts, etc.


How To Make File Executable in Linux

We will look at the different ways to make file executable in Linux.


1. Using Command Line

You can easily check if your file is executable or not, using ls -all command.

$ ls -all <file_path>

For example, the following command will display the file permissions of /home/ubuntu/test.sh

$ ls -all /home/ubuntu/test.sh
-rw-r--r-- 1 ubuntu ubuntu 1248 Sep 14 04:04 mysql_backup.sh

In the above output, -rw-r–r– indicates that file owner has read(r) & write(w) permission but not executable permission(x), indicated by rw-. It also indicates that both group and users other than owner have only read permission, indicated by r–.

The simplest way to make a file executable is to open terminal and run the chmod command with +x option, followed by your file path.

$ chmod +x file_path

Here is an example to make /home/ubuntu/test.sh file executable.

$ chmod +x /home/ubuntu/test.sh

In the above command, if you don’t mention full file path, chmod will look for file relative to your present working directory.

After that you can easily run it mentioning its path.

$ /home/ubuntu/test.sh

Now if you check file permission again, with ls -all command, you will see.

$ ls -all /home/ubuntu/test.sh
-rwxr-xr-x 1 ubuntu ubuntu 1248 Sep 14 04:04 mysql_backup.sh

You will see that owner, group and others have ‘x’ (for execute) in their permissions, indicating that they all have executable right for this file.

If you want to allow only owner to be able to execute but not the group or others, you can specify the permissions accordingly in chmod command.

$ chmod +rwxr--r-- /home/ubuntu/test.sh

$ ls -all /home/ubuntu/test.sh
-rwxr--r-- 1 ubuntu ubuntu 1248 Sep 14 04:04 mysql_backup.sh

If you want to remove the executable permission, use -x option with chmod instead.

$ chmod -x /home/ubuntu/test.sh

Now if you want to use a different string to run the command, you can add an alias to your file. For that purpose, run the following command first.

$ gedit ~/.bashrc

This will open bash profile. Add the following at its end.

alias <new name>='<full path to script>'

Replace <new name> with the alias name you want to use, and <full path to script> with file path to your executable script.

Here is an example.

alias myscript='/home/ubuntu/test.sh'

Save and close the file. On terminal, run the following command to apply changes.

$ source ~/.bashrc

Now onwards, you can simply use the alias name to call your script.

$ myscript


2. Using GUI

You can also make file executable via GUI. For this purpose, right click on your file and click Properties. You will see the following window. Click Permissions tab.

Then check the box Execute: [ ] Allow executing file as program or in Nautilus Program: [ ] Allow this file to run as a program. Click Close/Save/Ok button, depending on your Linux distribution, to apply changes.

make file executable in python

In this article, we have learnt how to make file executable in Linux for owner, group & other users. We have also learnt how to selectively allow only owner to be able to execute file. You can make a file executable as per your requirement.

Also read:

How to Recursively Change File Permission in Linux
How to Get Size of Directory in Linux
How to Calculate Time Difference in Python
How to Measure Time Taken By Python Program
How to Encrypt & Decrypt Files in Python

Leave a Reply

Your email address will not be published. Required fields are marked *