- 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 Execute Binary Files in Linux
- How to Execute Binary Files in Linux:
- Conclusion
- About the author
- Syeda Wardah Batool
- How to convert bash file to a binary executable
- 3 Answers 3
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 Execute Binary Files in Linux
Binary files or bin files are executable files in the operating system also known as non-text files. These files can contain anything in it like images, compiled files, metadata, chain of sequential bytes or a text file having encoded binary data.
In Linux and Unix-like operating systems, .bin files contain machine code in it and can be executed on the system. All the data encoded in binary files cannot be readable by humans. These files can store anything except text.
How to Execute Binary Files in Linux:
To execute a binary file in a system, all you need to do is work as a super user with all privileges and permissions.
To run binary files on a Linux system, we need to make it executable by accessing them using a terminal. It can be done by following 3 steps.
-
- Open the command-line prompt by pressing ctrl+alt+t.
- The next step is to run without giving permission.
Now, the file is ready to run on Linux system, again open the terminal and type the command:
In this file doesn’t open and shows permission denied message, use sudo in command and run it again:
Conclusion
The .bin files are the binary files that contain information to be executed in a system. They are encoded with machine code and cannot be readable. The article has shown how we can make binary files executable by giving them permission. These files cannot be executed without permission access and sudo privileges.
About the author
Syeda Wardah Batool
I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.
How to convert bash file to a binary executable
I created a binary executable from bash script on linux server through SHC. The binary created works fine on linux machines, but through mistake on Mac. How could I convert my bash file to binary executable that is able to run everywhere(ubuntu, CentOS, Mac, Cygwin)?
shc -v -r -T -f ir16fetcher.sh mv ir16fetcher.sh.x ir16fetcher
./ir16installer USAGE : ir16fetcher [the n th latest build - optional. Default 1] EXAMPLE: ir16fetcher jagger 2 EXAMPLE: ir16fetcher 167.116.6.155 REQUIRE: Please make sure conf file in installation folder ~/IRinstall/ir16 & ~/IRinstall/irmanager
./ir16installer -bash: ./ir16installer: cannot execute binary file
Linux binary wouldn’t work on Mac. Create the binary on Mac. BTW, why do need to create binaries for shell scripts?
3 Answers 3
I think it’s not gonna work
«The compiled binary will still be dependent on the shell specified in the first line of the shell code (i.e. #!/bin/sh), thus shc does not create completely independent binaries.»
You will have to do this for every architecture and operating system you need to support. In any case, there doesn’t really seem to be any benefits of using this method for distribution. It adds dependencies and complicates delivery, and I’m pretty sure whatever obfuscation the «shc» compiler implements is easily reversed.
if the goal here is to «hide» your source code, and then have the «hidden» copy of the code be executable on the Unix OSes you listed, then, encryption is really your only option.
I say this because encryption tools are available on every base Unix install. For your purposes, this is a very good thing as you wont have to download or configure anything additional. They’re just there, as part of the natural installation of the OS. One of such tools is called openssl.
To Encrypt your file/script with openssl:
echo precious-content | openssl aes-128-cbc -a -salt -k mypassword U2FsdGVkX1+K6tvItr9eEI4yC4nZPK8b6o4fc0DR/Vzh7HqpE96se8Fu/BhM314z
To Decrypt your file/script with openssl:
echo U2FsdGVkX1+K6tvItr9eEI4yC4nZPK8b6o4fc0DR/Vzh7HqpE96se8Fu/BhM314z | openssl aes-128-cbc -a -d -salt -k mypassword precious-content