How to Make a File Executable in Linux [3 Best Ways]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To make a file executable in Linux, follow these steps

  1. Open Terminal.
  2. Run ls -l to check current permissions.
  3. Run chmod u+x [filename] to set execute permission for the owner.
  4. For group or others, use chmod g+x [filename] or chmod o+x [filename].

To find out more about how to make a file executable in Linux and resolve its common issues, read the step-by-step guide below.

Are you struggling to run your scripts or programs in Linux because they aren’t executable? This post will show you how to make any file executable in Linux. You’ll learn different methods and practical steps, and I’ll cover common errors you might face. You can expect to find detailed explanations on using the chmod command, adding a shebang line, and even using the GUI. Plus, you’ll learn how to set executable permissions for the owner, a group, and all users.

What It Means to Make a File Executable?

Making a file executable means giving the file permission to be run as a program. In Linux, files typically have different types of permissions, like read, write, and execute. When a file is executable, you can run it directly from the command line or by double-clicking it in a file manager.

For example:

If you have a script that you wrote in a text editor, it won’t do anything if you try to run it unless you make it executable first. By changing the file’s permissions to include execute permissions, you tell the system that this file is meant to be run as a program.

How to Make a File Executable in Linux

To make a file executable in Linux, you have several options. Use the chmod command by navigating to your file in Terminal and running chmod +x [filename]. You can also add a shebang (#!) line at the top of your script, like #!/bin/bash, save it, then run chmod +x [filename]. Another method is to use the GUI: right-click the file, select Properties, and check the Execute box under Permissions. Now, your file is executable.

Here’s a breakdown of each of these methods:

1. Use chmod Command

Before learning to make a file executable in Linux, you should know the basics of file permissions. This includes read (r), write (w), and execute (x), which can be assigned to the owner, group, and others. The ls and -l command displays permissions, and the chmod command is used to modify them. Once you know about the file permissions for a certain file, use chmod to modify its access permissions. Here’s how to do it:

  1. Run the ll command to view the permission of the file before making it executable.
file permissions make a file executable in linux
  1. To make a file executable in Linux Terminal, you need to set the execute permission for the user group to which the file belongs. Here is the command to do that:
chmod u+x [filename]
  1. This command sets the execute permission (x) for the user (u) file group. You can also set the execute permission for the group (g) or others (o) by replacing u with g or o, respectively.
making it executable

2. Use Shebang Notation

Another way to make a file executable in Linux is by using the shebang (#!) notation. It’s a special sequence of characters that tells the shell what interpreter to use to run the script. Here’s how to add a shebang to make a file executable in Linux:

  1. Right-click the script file, then open it using the text editor app.
open text editor
  1. Add the shebang line (#!) at the top of the file, specifying the path to the interpreter. For example, if you want to use the Bash interpreter, the shebang line should be !/bin/bash
adding shebang
  1. Save the file with a meaningful name and the .sh extension.
save the file
  1. Once you’ve added the shebang to the file, head to the Terminal and navigate to the directory where the script file is located.
cd ~/directory-name
  1. Next, run the ls command to confirm whether the script file exists in that directory.
navigating to directory of file
  1. Run the following command to make the script file executable while replacing “script.sh” with the actual name of your script file:
chmod +x script.sh
  1. Again, run the ls command to see if the file has become executable. If it is, it’ll show in a different color.
making file executeable using command
  1. You can now execute the script by running ./script.sh in the Terminal. The shebang line ensures that the correct interpreter is used to execute the script. Here is the output:
executing script file

3. Use Linux GUI

You can also make a file executable in Linux using the graphical user interface (GUI). This method is very simple and easy for people who prefer to use the graphical representation of the Linux distros. Here are the steps to follow:

  1. Right-click on the file you want to make executable and select Properties.
right click to go to properties
  1. Then navigate to the Permissions tab. 
navigate to permissions
  1. From there, you can check the Execute box to make the file executable.
Check the execute box
  1. Once done, head to the Terminal window and run the script with the following command:
./filename
  1. Now, you should see the following output:
executing script file 1

Setting Executable Permissions for Different Users

1. Make the File Executable for the Owner

In Linux, making a file executable for the owner allows the file’s creator to execute it while restricting access to others. This is useful for personal scripts or applications that only the owner should run. The chmod command is used to change file permissions.

  1. Open the Terminal.
  2. Use the cd command to change to the directory where your file is located.
cd /path/to/your/file

Replace the /path/to/your/file with the directory name you want to move.

navigating to a directory 4
  1. Use the chmod command with the u+x option to add execute permissions for the owner.
chmod u+x filename

This command modifies the file’s permissions so that the owner can execute it.

making file executable for the owner
  1. Check the file permissions by running the command:
ls -l filename

The output should show an x in the owner’s permission set, indicating that the file is executable by the owner (e.g., -rwx——).

verifying the change in file permissions

2. Make the File Executable for a Group of Users

Making a file executable for a group of users allows members of a specific group to execute the file. This is useful in collaborative environments where multiple users need access to the same executable file. Linux uses group-based permissions to manage this.

  1. Access the terminal on your Linux system. Navigate to the directory containing the file:
cd /path/to/your/file
  1. Use the ls -l command to see the current group owner of the file.
ls -l filename

The group owner is listed in the output (e.g., -rw-r—– 1 owner group filename).

checking current permission of the file
  1. Use the chown command to change the group owner if needed.
sudo chown :groupname filename
changing group owner of the file
  1. Use the chmod command with the g+x option to add execute permissions for the group.
chmod g+x filename

This command modifies the file’s permissions so that the group can execute it.

adding execution permissions to the group
  1. Check the file permissions using the command:
ls -l filename

The output should show an x in the group’s permission set, indicating that the file is executable by the group (e.g., -rwxr-x—).

verifying the file permissions 1

3. Make the File Executable for Every Other User

Making a file executable for every other user allows all users on the system to execute the file. This is useful for publicly accessible scripts or applications that need to be run by anyone. However, it comes with security implications that need to be carefully considered.

  1. Launch your Terminal window and access the directory containing the file:
cd /path/to/your/file
  1. Run the following command to add execute permissions for others.
chmod o+x filename

This command modifies the file’s permissions so that all users can execute it.

adding execution permissions to other users
  1. Check the file permissions using ls -l.
ls -l filename

The output should show an x in the others’ permission set, indicating that the file is executable by others (e.g., -rwxr-xr-x).

verifying the execution permission chagnes

5 Common Issues When Making a File Executable

When making a file executable in Linux, you may encounter a few common issues or errors. Here are five of them, along with their easy solutions to help you troubleshoot:

  • 🛡️ Executing Commands with Superuser Privileges: If you encounter a “Permission denied” error, it’s often because the command requires elevated permissions. In such a case, use the sudo command to execute the file as a superuser. Here’s an example of how you do it: sudo ./file_name.
  • 📂 Handling Read-Only File Systems: If you experience a “File is in a read-only file system” error, it means you’re trying to modify a file in a read-only location. You can either move the file to a different directory or remount the file system as read-write. Here’s how you remount the file system: sudo mount -o remount,rw /.
  • 🚨 Reacquiring Corrupted Files: If you encounter a “File is corrupted” error, the file you’re trying to use might have been damaged or improperly downloaded. Consider redownloading the file or acquiring a different copy to resolve this issue.
  • 🔍 Installing Missing Commands: A “Command not found” error typically suggests that the command you’re trying to use is not installed on your system or not available in your current PATH. To fix this, install the necessary command or add the directory containing the command to your PATH variable. Here’s an example of installing a command: sudo apt-get install command_name.
  • 📝 Renaming Files with Correct Extensions: If you’re experiencing a “File has no extension” issue, it’s likely because the file you’re trying to use lacks a proper extension. To resolve this, rename the file and add an appropriate extension. For instance, if it’s a shell script, you should rename it with a “.sh” extension. You can do this with the following command: mv file_name file_name.sh.

Key Takeaways

In this article, I’ve covered how to make a file executable in Linux using the chmod command, adding a shebang line, and using the GUI. I also showed you how to set executable permissions for the owner, a group, and all users.

If you found this guide helpful, you might want to explore about managing files in Linux. For additional skills:

Frequently Asked Questions

How to change file ownership in Linux?

To change file ownership in Linux, use the chown command. Here’s the syntax for using this command chown [new_owner]:[new_group] [filename]. The [new_owner] argument is the new owner of the file, and the [new_group] argument is the new group to which the file will belong. If you want to keep the same group, you can omit the :[new_group] part of the command.

How do I check if a file is executable in Linux?

To check if a file is executable in Linux, you can use the ls command with the -l option. This will show you the file permissions for the file. Here’s an example, ls -l [filename]. You will see an x in the file permissions if the file is executable. For example, if the file is named “myscript.sh”, and it’s executable, you will see something like this -rwxr-xr-x 1 user user 1234 Apr 28 10:00 myscript.sh. The first character (-) indicates that it’s a regular file. The next three characters (rwx) indicate the permissions for the owner of the file. The next three characters (r-x) indicate the permissions for the group, and the last three characters (r-x) indicate the permissions for everyone else. The x in the second set of permissions (rwx) indicates that the file is executable.

What to do if the executable file does not have an extension?

Many file formats in Linux do not have a file extension, so it can be difficult to identify the appropriate extension to add. In such cases, it is necessary to determine the type of file and add an appropriate extension manually. There are several methods to identify file types, such as the file command, which can provide information on the file type, its encoding, and other details. Once the file type is identified, the appropriate extension can be added using the mv command.

How do I make a file executable in Linux by default?

To make a file executable in Linux by default, you can set the default file creation mask (umask) to 022. This will ensure that any new files you create have the executable bit set by default. To set the default umask, you can add the following line to your .bashrc or .bash_profile file umask 022. This will set the default file creation mask to 022, which means that new files will have permissions of 644 (readable and writable by the owner, readable by everyone else). If you want new files to be executable by default, you can set the umask to 002, which will give new files permissions of 755 (readable, writable, and executable by the owner, readable and executable by everyone else).

Can I make a directory executable in Linux? 

Yes, it is possible to make a directory executable in Linux. This means you can make a directory executable to execute commands within the directory. However, it is not recommended as it can pose security risks, especially if the directory contains sensitive files or is accessible to other users. In general, directories should be set to read and write permissions only, and executable permissions should be restricted to specific files within the directory as needed.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Use Less Command for Searching Patterns [5 Best Ways]

Next Post

How to Read a File Line by Line in Bash [5 Easy Methods]

Read next