How to make a file executable? [duplicate]
How can I make my file so that I can double click on it, and it runs. It is a .sh script, but I also have files that say:
in the description of what they are.
I can’t run any of these from terminal, or by double clicking.
If possible, I would like a way using either the GUI or a Terminal, but not a combination of the two.
Here is a screenshot of what I see when I right click then go on properties. The file first:
And the shell script here:
NB: I accept that this is a duplicate (I was looking for it, and couldn’t find it, so asked + answered it, hoping that I would find it) however, I don’t think the question about .desktop files is a duplicate.
1 Answer 1
There are two ways of making a file executable:
GUI Method:
Go to the permissions tab, then tick the box Execute: [✓] Allow executing file as program.
Command line method:
Note that chmod does also have some more advanced options. It accepts three groups of options, represented as — — — . The first set of — is User. The second is Group and the last is Other (everyone else).
r stands for Read, w for Write and x for eXecute.
To allow everyone to read it, but only Group to execute and User to read and write it would be -rw- rx- r— . This would be added to the command as:
chmod also can do this in numbers. It is based on binary.
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 would be 400 + 040 + 004 and 010 and 200
That adds up to 600 + 050 + 004 = 654 .
You could then run the command.
to set it. So to set all permissions you can run:
Finally, you can do:
To take all permissions away from everyone.
To add read and write for the user, without affecting any other permissions (e.g. Execute permissions).
This website has a very useful little tool, 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
700 — Owner has all
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!
Creating executable files in Linux
One thing I plan to be doing is writing (painfully simple) Perl scripts, and I’d like to be able to run them without explicitly calling Perl from the terminal. I appreciate that, to do this, I need to grant them execute permissions. Doing this with chmod is easy enough, but it also seems like a slightly laborious extra step. What I would like is one of two things:
Firstly, is there a way to set the execute flag when saving a file? Currently I’m experimenting with gedit and geany, but would be willing to switch to a similarly- (or better-) featured editor if it had this capability.
Failing that, is there a way to declare that all files created in a particular directory should have execute permissions?
My umask is set to 022, which should be OK, as far as I understand, but it would appear that the files are created as text files (with 666 default permissions) rather than executable files (with 777 default permissions).
Perhaps I’m just being lazy, but I figure there must be a more convenient way than chmodding every single script one creates.
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?
3 Answers 3
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.
There are two ways of making a file executable:
GUI Method:
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:
You can either use:
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
Finally, you can do:
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
700 — Owner has all
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!
How to make a file executable in linux?
To give a little more context, I think that this question stems from the distinction between executable and non executable files on windows, characterized by the .exe extension.
In linux, every file can be an executable.
Let’s see what happens when you try to execute a file. First, we need to have a file. We can use the echo command and redirect its output to create a new file.
Note the lack of extension in the filename. We could have named it hello.txt, or hello.sh, it’s entirely upto us, the extension mostly doesn’t matter. Unlike windows, you don’t need a special extension to make a file executable.
Now, we’ll execute the file.
Let’s go over what happened.
To execute a file all we need to do is enter the path of the file on the command prompt. We’ll also need execute permissions on the file, like to read a file we’d need read permissions. Note that setting or removing the execute permissions does not make a file executable or non-executable, but it just takes away or gives us the ability to execute it.
What exactly happens when we ‘execute’ a file is that the contents of the file are sent to the program located on the first line. In this case, it’s /bin/bash.
This means that all contents of the file are being sent to bash. So it’s as if you were typing those commands into the shell. Therefore, in the file we’ve created it ran the echo command.
Please note that there’s nothing special with ./, it just simply means ‘look for the file in the current directory’. It could have been any path, for example
And you could also have some other program handle the execution. For example, consider the following file in which I’ve set the first line to be the location of python binary, which I found via the command whereis python. It may be different on your system.
You could also set a .doc file’s permissions as executable, or a .jpg file. It’s perfectly legal. But when you try to execute those, their contents will be sent to shell to be parsed as shell commands. But they aren’t shell commands, so you’ll get an error.