Error: Cannot Execute Binary File

Abid Ullah Feb 02, 2024
  1. Linux Binary File
  2. Resolve the cannot execute a binary file Error in Linux
Error: Cannot Execute Binary File

In the article, we will learn how to execute a binary file in Linux. We will also learn how to resolve the error if Linux fails to execute a binary file.

Usually, this error occurs when we run shell scripts in Linux. This article will provide all the working methods to help you resolve this problem.

Linux Binary File

In programming, binary means two, and the binary file has two characters (1,0). Binary files are used to store information and data.

In Linux, a binary file extension is .bin. A binary file can be executed in Linux by the syntax ./file.bin.

When we execute a binary file in a Linux system, sometimes we come up with an error cannot execute a binary file. This error means the Linux system does not recognize it as a shell script or executable file.

Let’s see the methods of how to resolve this error in Linux.

Resolve the cannot execute a binary file Error in Linux

The cause of this could be a mismatch in the architecture type of the binary file. The other reason could be the user does not have permission to execute the binary file.

So in the Linux system, we will give you a few solutions to eliminate this problem. Try using one of the following solutions, and then check if the error is resolved or not.

Check the Architecture of the File

When facing this error, the first thing you need to do is to ensure that you have got the right binary file. Because if you have a 32-bit binary file executing it on the 64-bit system, it won’t execute and will give you the error.

The binary files for 32-bit do not work on 64-bit systems and vice-versa. We can check the architecture of any file in Linux by using the following command.

Syntax of the Command:

$ file filename

Command Used to the Architecture of the File:

$ file test.sh

Check the Architecture of the File

As shown in the image, we used the terminal command file test.sh. We can see in the output the architecture of the file named test.sh is displayed.

In this way, we can also find the architecture of a binary file.

In case the architecture of your binary file does not match the system you run it on, you need to run your binary file to the matching architecture system.

Check Your Binary File Format

We run binary files on Windows, Linux, and macOS systems. The binary files made for the Windows system will not run other operating systems and vice-versa.

These binary files have different formats, which can help the users to distinguish between them. It won’t work if you run a binary file made for Windows or macOS on a Linux system.

You must run a binary file made for Linux on a Linux operating system.

Install GCC and Gfortran Packages in Linux

If these two packages, GCC and Gfortran, are not installed in our Linux system, then this could be a reason that our Linux system cannot execute a binary file. These packages (GCC and Gfortran) must be installed for several binary files to compile and execute properly.

We can install these libraries by using the following commands in the terminal.

Command Used to Install the Gfortran Package:

$ sudo apt-get install gfortran

Install the Gfortran package in Linux System

Command Used to Install the GCC Package:

$ sudo apt-get install build-essential

Install the GCC package in Linux System

After installing these two packages in a Linux system, the cannot execute binary file error could be resolved.

Try to Un-Compress the Binary File

Sometimes, we compress the binary files to make them easier to share over the internet. If the binary file you are executing is already compressed, then try uncompressing the file.

We can un-compress a binary file by using the following command in the terminal.

Command Used to Uncompress the File:

$ xz -d ./filename

Check the Binary File Permissions

Another reason for a binary file not executing on a Linux system could be that the user does not have permission to change or read the file binary file. We can fix this by giving execution permission to the binary file using the following command in the terminal.

Command Used to Give Execution Permission to the File:

$ chmod +x filename

Once the permissions are set, we can run the binary file using this command in the terminal:

$ ./filename

Use the dos2unix Command to Execute the Binary File

The best solution to resolve the cannot execute a binary file is to use the dos2unix command in Linux. This command can help us to run binary files made for DOS on our Linux system.

Command Used to Convert DOS Binary File to Unix Format:

$ dos2unix filename.bin

After using this command in the terminal, the DOS format binary file will be converted to Unix format. Then, our Linux system can execute the binary file.

This article has explained all the possible solutions to resolve the cannot execute a binary file error in Linux. We hope you find this Linux article helpful in understanding how to execute a binary file in Linux.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn

Related Article - Linux Error