Home Learn Linux How to make a file executable in Linux

How to make a file executable in Linux

by Brandon Jones
make file executable Linux

If you have previously interacted with an operating system like Windows, then the following context should make perfect sense. Such operating system environments support two types of files. The first type is the executable ones, and the second type is the ones that are non-executable. The executable files are associated with a signature file extension like “.exe”.

Making a file executable in Linux

On the other hand, when we migrate into the Linux ecosystem, the non-executable and executable file rule does not apply. Every file can be given the executable privilege. To understand how this concept works, we need a sample file to reference.

The terminal/command-line approach

To create such a file, we can use the inbuilt echo command. It is applicable on the Linux terminal. This command is useful in creating a command string and redirecting it to a file.
Open your Linux terminal and run the following command sequence.

$ echo 'echo hello FossLinux user, welcome to this tutorial session' >> greetings
$ cat greetings

From the above command sequence, we used the echo command to create the literal string “echo hello Fosslinux user, welcome to this tutorial session”. Afterward, we stored this literal string into a file named “greetings”. As you might have noted, with Linux, it is possible to create a file without giving it a file extension like .exe or .txt. This approach of creating files is not possible in other operating system environments.

A file existing without an extension in Linux makes it possible to grant it an execution privilege whenever the need arises. After the “cat greetings” command executes, the terminal outputs the literal string we stored inside the “greetings” file from the above command sequence.

echo hello FossLinux user, welcome to this tutorial session

The literal string needs to start with the echo command for the Linux environment to interpret it correctly. Otherwise, you might get an error like “command not found” when we later make the file executable. The flexibility of the Linux operating system environment becomes viable when we can make this “greetings” file executable without referencing the cat command to access its content.

To make the file executable, we will use the “chmod” command as demonstrated below.

$ chmod +x greetings

Afterward, we only need to call this “greetings” file in the following manner to access its content.

$ ./greetings

The expected output should be similar to the following:

hello FossLinux user, welcome to this tutorial session

As you have noted, the above output does not begin with ‘echo’. The terminal environment does not interpret echo as an output but as a command needed to display the resulting terminal output.

Under the hood of this file execution process

Before the execution of any file occurs in a Linux environment, the first step is to link the path of that would-be executable file to the Linux terminal environment. The next step is to determine the type of permission the file needs. For example, we needed to access the content of the created “greetings” file for the case we demonstrated.

In this scenario, we needed to work with “read” permissions to access and output the contents of this file. It makes the use of the “chmod +x” command necessary. Assigning or removing a file’s execution permission does not make that file permanently executable or permanently in-executable. We are just giving the file an execution privilege which can also be taken away.

The use of “./” before the “greetings” file tells the terminal program where to find the file scheduled for execution. You can also use “sudo” before “./greetings” to grant the execution processes with the needed privileges. In the above-discussed example, we used echo to handle the execution of the literal string “hello FossLinux user, welcome to this tutorial session”. You can use another program like Python’s print for this execution.

You will first need to set the program’s location binary as a header on the file you wish to execute. For this case, if we were to use Python’s print function, the content of the “greetings” file will look something similar to the following:

#!/usr/bin/python

print "hello FossLinux user, welcome to this tutorial session"

Executing this file with the “./greetings” command will yield the same results as the ones from using the echo command.

The GUI approach

If you do not want a technical route to the way you make your files executable, Linux’s graphical user interface is always a good place to start. First, locate the file you wish to make executable by browsing to its location. Use the computer mouse to right-click on this file and select the “properties menu”.

selecting the properties menu option on a file you wish to make executable

selecting the properties menu option on a file, you wish to make executable

The next screen will have three aligned menu options. Select the “Permissions” menu option.

GUI method of making a file executable on a Linux environment

GUI method of making a file executable on a Linux environment

On this window, check the box that states “Allow executing files as programs”.
With these simple GUI steps, you will have made your targeted file executable.

Chmod flexibility

Chmod is a short form for “change mode”. It deals with permission access control for both files and directories on your Linux operating system. For instance, since we have already dealt with making a file executable, you might also wish to know how to take these execution privileges away from everyone.

In this case, you might want to familiarize yourself with some absolute modes associated with the chmod command. Numeric numbers represent these modes, and their usage follows the following syntax rule:

chmod absolute_mode name_of_file.file_extension

Another chmod usage in file execution

  • chmod 775 your_file_name.file_extension: With this mode, anyone can execute the created or existing file. However, it is only the file owner that can write or add data to that file.
  • chmod 777 your_file_name.file_extension: With this mode, every Linux system user has equal permission to execute an existing or created file.
  • chmod 0010 your_file_name.file_extension: It is only defined group members of a Linux system that will execute the presented file.
  • chmod 0100 your_file_name.file_extension: It is only the file owner or the individual who created the file on the Linux system that will execute it exclusively.
  • chmod -777 your_file_name.file_extension: This command takes away the file execution privilege from all users of that Linux operating system.

With this extra info on the chmod command, you should be confident in its usage in making your files executable under the Linux operating system environment.

Final note

The simple rule to make a file executable in Linux entails the following steps.

  • Have access to your Linux terminal
  • Use your Linux terminal to browse to the location of the targeted file.
  • Once you find the targeted file, note its name plus the associated file extension, if any. The file extension can be “.bin” or “.run”.
  • Afterward, use the following command syntax to make that file executable.
sudo chmod +x name_of_file.file_extension

For example, sudo chmod +x new_file.bin

After you have just made this file executable, you might want to install it if it is a “bin” file or just run it if it is a “run” file. In either case, the following command approaches will get the job done.

For a .bin file, remember to use ./my_file_name.bin

For a .run file, remember to use ./my_file_name.run

If you run into any errors trying to install or run the created executable file with the above command approaches, precede the execution of these commands with “sudo”. It will give the current user of the Linux system the needed execution privileges.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.