- Linux: Mark Program File as executable
- Typical Error Messages
- Make the Program executable
- Run Program
- About the Author
- Related Topics
- Windows: How to run a Program as Administrator — Once, Always or with Shortcut
- How to send E-Mails with EXE Attachment
- Windows Batch Script: Computer Shutdown
- Linux: Program file not found although existing (No such file or directory)
- PHP: File Download Script
- Lazarus: Open File or Document platform-independent
- Rewrite Text Files with a fixed Line Length
- Important Note
- Participate
- How to make a file (e.g. a .sh script) executable, so it can be run from a terminal
- 4 Answers 4
Linux: Mark Program File as executable
Many who are not familiar with the Linux world and who are used to run programs only from Windows are surprised when a program or batch script is not immediately opened and executed after a double-click from a folder or a call from the terminal.
Those who are always using the Software Center or other Linux package managers for installing applications will not often be confronted with these problems, because in these cases, Linux and its system administration take care of everything concerned. Therefore, the following steps are usually necessary only if the programs come from other sources or if you want to start your own programs.
Typical Error Messages
Typical error messages are «Could not display ProgramName», «There is no application installed for executable files» or «Permission denied» in the terminal.
Make the Program executable
On Linux, each file has rights for certain user groups. For example, «r» (read) if a file can be read by a user group or «w» if a file can be written to or changed by a group (write). If you enter «ll» into the terminal, you will typically see all these flags in front of the file name.
The flag «x» (executable) marks a file that is executable. If we try to execute a program file that does not have an x-flag for its own user group, the above error messages will appear instead of executing the application.
With the command «chmod» (Change Mode) we can simply set the x-flag of a file from the terminal:
To do this, we need to navigate to the appropriate program file in the terminal and then we can enter «sudo chmod +x» followed by the name of the program file. Since root privileges are required for this process, we must preface the command «sudo» and then enter our password to execute the command.
The «+x» causes the «x» flag to be added to the file flags. Likewise, the flag can be removed again using «-x». Correspondingly, we can change the read and write permissions/rights of a file via «+r», «-r», «+w» or «-w».
Alternatively, we can also click on the file with a right click and make the appropriate settings under Properties > Permissions from the file info dialog.
Run Program
After marking the application file as executable, it can be easily opened from a folder with a simple click or a double-click (depending on the system) or from the console/terminal with the command «./ProgramName».
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? — sttmedia.com/contact
Show Profile
Related Topics
Windows: How to run a Program as Administrator — Once, Always or with Shortcut
How to send E-Mails with EXE Attachment
Windows Batch Script: Computer Shutdown
Linux: Program file not found although existing (No such file or directory)
PHP: File Download Script
Lazarus: Open File or Document platform-independent
Rewrite Text Files with a fixed Line Length
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
How to make a file (e.g. a .sh script) executable, so it can be run from a terminal
I have a script.sh file and type of this file is shellscript file. I want to make this file as application/x-executable file. How can I make it?
It is not a duplicate, because I have asked specifically about making it application/x-executable. The other question just asks for opening sh file in terminal.
4 Answers 4
You can mark the file as executable:
You can then execute it like this:
If you want to use a different command to start it, you can add an alias:
Add this at the end of the file:
Open a new terminal session or type source ~/.bashrc in your terminal to apply. Then simply use the new name to start the script.
Do you know how to use sudo command after entering the command as: «alias command1 = ‘/home/user_name/dir/script.sh’. In mine, it works without sudo, but not with it.
@user1993 Generally, using ./filename.sh specifies a file in the current directory and using filename.sh specifies a file in the current directory or any directory of PATH. The first usage removes any uncertainty as to which file is accessed. In this case, you are attempting to execute the script with bash or another interpreter (by virtue of assumed #!/bin/bash as first line in your script) just by entering the filename. This usage requires the directory is specified. Alternatively, you can try bash filename.sh which seems to work with unspecified directory.
There are two ways of making a file executable:
Right-click the file and select Properties. Go to the permissions tab, then tick the box Execute: [ ] Allow executing file as program or in Nautilus Program: [ ] Allow this file to run as a program in Thunar.
Terminal / Command method:
chmod +x filename.extension
chmod +x /path/to/your/filename.extension
chmod does also have some more advanced options:
The spaces are to show that it is split up: — rwx — —
The first set of — is User. The second is Group and the last is Other (anyone else)
r stands for Read, w for Write and x for eXecute.
So to allow everyone to read it, but only Group to execute and User to read and write it (but for some reason not execute) would be:
-rw- rx- r— But this would be added to the command as:
chmod +rw-rx-r— /path/to/file.extension
chmod also can do this in numbers. It is based on binary (I think, as it is 1,2 and 4)
So there are these numbers:
Execute by user is 100 . Execute by group is 010 . Execute by other is 001 .
Write by user is 200 . Write by group is 020 . Write by other is 002 .
Read by user is 400 . Read by group is 040 . Read by other is 004 .
Then you add these together to get the desired combination.
So to allow everyone to read it, but only Group to execute and User to write it (but for some reason not execute) would be:
400 + 040 + 004 and 010 and 200
That adds up to 600 + 050 + 004 = 654.
You could then run the command.
chmod +654 /path/to/file.extension to set it.
And to set all permissions you can type:
chmod +rwxrwxrwx /path/to/file.extension
Or (this is a bit easier to write, but harder to remember each one):
chmod +777 /path/to/file.extension
chmod -777 /path/to/file.extension
To take all permissions away from everyone.
chmod +300 /path/to/file.extension
To add read and write for user, without affecting any other permissions (e.g. Execute permissions).
This website has a very useful little grid checkbox thing, whereby you can tick the options you want and it gives you the command:
However, not all the possible combinations are sensible to use; the main ones that are used are the following:
755 — Owner has all, and Group and Other can read and execute
644 — Owner can read and write, and Group and Other can read
600 — Owner can read and write
And, if you’re using non-trivial user groups:
775 — Owner can read and write, and Group and Other can read
770 — Owner and Group have all, and Other can read and execute
750 — Owner has all, and Group can read and execute
664 — Owner and Group can read and write, and Other can just read
660 — Owner and Group can read and write
640 — Owner can read and write, and Group can read
777 and 666 are rarely used, except in /tmp.
Thanks Ilmari Karonen for pointing out the ones in common usage!