- How to Make a File Executable in Linux terminal?
- Make a file executable in Linux
- Method 1: Make file executable for everyone
- Method 2: Make file executable only for certain user or group
- Method 3: Use the octal numbers
- Conclusion
- How To Make A File Executable In Linux
- Method 1: Using The Command Terminal
- Method 2: Using the GUI
- Understanding How File Execution Works
- Additional Information
- Conclusion
- How to Make Bash Script Executable Using Chmod
- Step 1: Creating a Bash File
- Step 2: Writing a Sample Script
- Step 3: Executing the Bash Script
- Step 4: Set Executable Permissions to Script
- Step 5: Running Executable Script
- Another Example
- Conclusion
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.
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:
With that, you can execute said file from the terminal:
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.
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:
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.
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:
$ 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:
$ 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:
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].
$ 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.
How To Make A File Executable In Linux
If you’re someone with a background in a Windows operating system, chances are you’re aware of the “.exe” files. These files are referred to as executable files because their purpose is to execute a series of commands that make up the whole program.
However, when you make the change to a Linux distribution, this division of executable vs. non-executable does not apply. Any file can be made executable in Linux since it does not require a certain extension to be declared as an executable.
This provides a lot of flexibility when it comes to file management.
If you’re someone looking to learn how to make a file executable in Linux, then this guide is meant for you. We’ll provide a step-by-step solution on how you can make any file executable, either using the CLI or the GUI method.
Let’s take a look at the steps.
Method 1: Using The Command Terminal
The first method makes use of the Command Terminal. You can make any file executable by typing certain commands in the Terminal.
Although we’re going to be using Ubuntu 20.04 LTS in this guide, the steps shouldn’t be any different for other Linux distributions. With that said, just follow the steps explained with the help of an example.
First and foremost, open the Command Terminal on your system. The shortcut for Ubuntu is Ctrl + Alt + T.
For this guide, we’ll create a sample file using the echo command by using the following command:
This command will create a string literal with the text “This is a guide on how to make a file executable in Linux” and store it in a file called Test1.
To view the content of the file, type the following command:
You should notice that the file doesn’t end with an extension name. This means that you have the option to make the file executable.
In order to make Test1 an executable file, we’ll use the following command:
In case of a file extension, the command becomes:
Now you can call your file by typing its name in the Terminal as follow:
You can also execute the command in the following manner:
This will provide appropriate permission for the file to be executed.
If the CLI method seems complicated to you, worry not as you can achieve the same results with the help of the GUI.
Method 2: Using the GUI
Unlike the CLI method, the GUI method is much less daunting and simplified to understand what’s going on.
Follow these steps to make a file executable using the GUI.
Start by navigating to the file of your choice. Once you’ve navigated to the file, right-click on it and select “Properties”. A new window should appear.
Once a window opens, click on the Permissions tab.
In the Permissions tab, you should see an option titled “Allow executing file as program.”
You should now have the desired file in an executable format if you followed the steps correctly.
Understanding How File Execution Works
Learning how file execution works in Linux has its benefits as it provides more flexibility when it comes to an understanding of how the file works.
In Method 1, we used the command chmod +x. This was necessary in order to make the file executable since the file required “read” privileges. The “./” tells the Terminal to search for the location of the file.
Aside from Method 1, there are other ways to use the $ chmod command. This flexibility makes $ chmod extremely valuable. A list of options for the $ chmod command is given below:
- $ chmod 775 . This mode allows anyone to execute the file. However, only the owner of the file has permission to write in that file.
- $ chmod 0010 . Only users of a group will be allowed to execute the file.
- $ chmod 0100 . Permission to execute the file belongs exclusively to the user.
- $ chmod 777 . Provides permission to execute the file to all Linux users.
- $ chmod -777 . Doesn’t allow any user to execute the file.
Additional Information
Although the $ chmod command works for files without extension type, it should be noted that you’ll need to specify the file type in case it’s mentioned. For example, if you are dealing with a file that has a .run or .bin extension. The syntax for the execution command would be:
Additionally, make sure you have the correct name, file type, and file location before making any file executable.
Conclusion
If you followed the steps in the guide correctly, then the good news is you are now aware of how to make a file executable in Linux.
This guide covered different methods to make the file executable in Linux. We started by explaining the method involving the Command Terminal and followed up with the method to achieve the same with the help of the Graphical User Interface(GUI). We also covered additional uses of the $chmod command and the different permissions.
With this, we wish you all the best in your journey to master Linux.
How to Make Bash Script Executable Using Chmod
In this tutorial, I am going through the steps to create a bash script and make the script executable using the chmod command. After that, you will be able to run it without using the sh or bash commands.
Step 1: Creating a Bash File
The first step is to create a new text file with .sh extension using the following command.
Step 2: Writing a Sample Script
Open the newly created file using any of your favorite editors to add the following bash script to the file.
$ vim hello_script.sh #!/bin/bash echo "Hello World"
Save and close the file using ESC +:wq!
Step 3: Executing the Bash Script
There are two ways to run the bash file. The first one is by using the bash command and the other is by setting the execute permission to bash file.
Let’s run the following command to execute the bash script using bash or sh command.
Step 4: Set Executable Permissions to Script
The second way to execute a bash script is by setting up the executable permissions.
To make a script executable for the owner of the file, use u+x filename.
To make the file executable for all users use +x filename or a+x filename.
Step 5: Running Executable Script
After you have assigned the executable permissions to the script, you can run the script without bash command as shown.
Another Example
In the following example, I am going to write and execute a bash script to take a backup from source to destination.
$ vim backup_script.sh #!/bin/bash TIME=`date +%b-%d-%y` DESTINATION=/home/ubuntu/backup-$BACKUPTIME.tar.gz SOURCE=/data_folder tar -cpzf $DESTINATION $SOURCE
Save and close the file using :wq! and give it the executable permissions for all users using the following command:
Conclusion
At the end of this tutorial, you should be familiar with how to set a script executable in Linux.
I hope you enjoyed reading it, please leave your suggestion in the below command section.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks