Gaming industry under DDoS attack. Get DDoS protection now. Start onboarding
  1. Home
  2. Developers
  3. How to Make a File Executable in Linux

How to Make a File Executable in Linux

  • By Gcore
  • September 18, 2023
  • 3 min read
How to Make a File Executable in Linux

In the world of Linux, mastering the terminal is a rite of passage for every user. One of the fundamental skills you’ll need is the ability to make a file executable. Whether it’s a custom script or a program you’re testing, knowing how to give it the right permissions is essential. In this guide, we’ll walk you through the straightforward steps to make any file executable in Linux, ensuring you have the power to run your creations seamlessly.

What is File Executable

In Linux and other Unix-like operating systems, an executable file is one that can be run as a program. When we say, “making a file executable,” we refer to setting certain permissions on the file so that it can be executed as a standalone program or script. Here’s a breakdown:

  1. File Permissions. In Linux, every file has permissions that determine who can read, write, or execute the file. These permissions are categorized for three types of users: the owner of the file, the group associated with the file, and everyone else.
  2. Executable Bit. To make a file executable, you typically set the “execute” bit for the desired user categories. This is done using the chmod command in the terminal.
  3. Scripts and Binaries. Both scripts (like shell scripts with a .sh extension or Python scripts with a .py extension) and binary files (compiled from languages like C or C++) can be made executable.
  4. Running an Executable. Once a file is executable, you can run it from the terminal. If the file is in your current directory, you’d typically prefix it with ./, as in ./my_script.sh.

How to Make a File Executable

Let’s walk through the steps to make a file executable in Linux. Here are the step-by-step procedure:

#1 Create or Identify Your File

Before you can make a file executable, you need to have a file in mind. This can be a script (e.g., Bash, Python, Perl) or a binary program. For this guide, let’s assume you have a Bash script named my_script.sh with the following content:

#!/bin/bashecho "Hello from the script!"

#2 Check the Current Permissions

Before changing permissions, it’s a good idea to check the current permissions of the file. You can do this using the ls command:

ls -l my_script.sh

The output will look something like:

-rw-r--r-- 1 username groupname 49 Sep 18 12:00 my_script.sh

In this output, -rw-r–r– represents the file’s permissions. The absence of an x (execute) permission indicates that the file is currently not executable.

#3 Make the File Executable

You can make the file executable using the chmod command. The simplest way to make a file executable for the user, group, and others is with the following command:

chmod +x my_script.sh

#4 Verify the Permissions

After setting the execute bit, it’s a good practice to check the permissions again to ensure they were correctly set:

ls -l my_script.sh

The output should now reflect the executable permission:

-rwxr-xr-x 1 username groupname 49 Sep 18 12:05 my_script.sh

Note the x characters, indicating that the file is now executable.

#5 Execute the File

With the file now being executable, you can run it. Since it’s a script, you can execute it from the directory it’s located in with:

./my_script.sh

The output will be:

Hello from the script!

Tips

  • The #!/bin/bash line at the beginning of the script is called a “shebang.” It indicates which interpreter should be used to execute the script. This is essential for the system to understand how to run your script.
  • You can also give execute permissions to only the file owner, group, or others using chmod u+x, chmod g+x, and chmod o+x respectively.
  • Always be careful when making files executable, especially if you did not write them or if they come from untrusted sources. Executing malicious scripts can compromise the security of your system.

That’s it! You’re now able to make a file executable in Linux. Mastering file permissions in Linux is a significant step towards fully embracing the platform’s flexibility and power. As you proceed, always remember to handle executable permissions with care to maintain the safety and integrity of your system. Well done, and here’s to many more Linux adventures ahead.

Conclusion

Looking to deploy Linux in the cloud? With Gcore Cloud, you can choose from Basic VM, Virtual Instances, or VPS/VDS suitable for Linux:

Choose an instance

Related articles

What's the difference between multi-cloud and hybrid cloud?

Multi-cloud and hybrid cloud represent two distinct approaches to distributed computing architecture that build upon the foundation of cloud computing to help organizations improve their IT infrastructure.Multi-cloud environments involve us

What is multi-cloud? Strategy, benefits, and best practices

Multi-cloud is a cloud usage model where an organization utilizes public cloud services from two or more cloud service providers, often combining public, private, and hybrid clouds, as well as different service models, such as Infrastructur

What is cloud migration? Benefits, strategy, and best practices

Cloud migration is the process of transferring digital assets, such as data, applications, and IT resources, from on-premises data centers to cloud platforms, including public, private, hybrid, or multi-cloud environments. Organizations can

What is a private cloud? Benefits, use cases, and implementation

A private cloud is a cloud computing environment dedicated exclusively to a single organization, providing a single-tenant infrastructure that improves security, control, and customization compared to public clouds.Private cloud environment

What is a cloud GPU? Definition, types, and benefits

A cloud GPU is a remotely rented graphics processing unit hosted in a cloud provider's data center, accessible over the internet via APIs or virtual machines. These virtualized resources allow users to access powerful computing capabilities

What is cloud networking: benefits, components, and implementation strategies

Cloud networking is the use and management of network resources, including hardware and software, hosted on public or private cloud infrastructures rather than on-premises equipment. Over 90% of enterprises are expected to adopt cloud netwo

Subscribe to our newsletter

Get the latest industry trends, exclusive insights, and Gcore updates delivered straight to your inbox.