Commands.page Logo

How to Make a Script Executable in Ubuntu Linux

In Ubuntu Linux, running a script requires specific file permissions to ensure security and proper execution. This article explains the specific command needed to change these permissions and allows users to run their custom scripts directly from the terminal. We will cover the syntax, usage examples, and how to verify the changes were successful.

The Command to Change Permissions

The command used to modify file permissions in Ubuntu is chmod. To make a script executable, you need to add the execute permission flag to the file. This is done using the +x argument followed by the name of the script file.

Syntax and Example

The basic syntax for making a file executable is:

chmod +x filename.sh

For example, if you have a bash script named backup.sh, you would type the following command into your terminal:

chmod +x backup.sh

Verifying the Permissions

After running the command, you can verify that the permissions have changed by listing the file details. Use the ls -l command followed by the filename:

ls -l backup.sh

Look for the x character in the permission string. It indicates that the file now has execute permissions for the user, group, or others, depending on the specific settings.

Running the Script

Once the script is executable, you can run it by specifying the path to the file. If you are in the same directory as the script, use the following format:

./backup.sh

This ensures the shell knows to execute the file from the current directory rather than searching system paths.