Make execute file linux

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.

enter image description here

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:

enter image description here

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!

Источник

How To Make A File Executable In Linux

If you’re someone with a background in a Windows operating system, chances are you’re aware of the “.exe” files. These files are referred to as executable files because their purpose is to execute a series of commands that make up the whole program.

Читайте также:  Linux limits conf open files

However, when you make the change to a Linux distribution, this division of executable vs. non-executable does not apply. Any file can be made executable in Linux since it does not require a certain extension to be declared as an executable.

This provides a lot of flexibility when it comes to file management.

If you’re someone looking to learn how to make a file executable in Linux, then this guide is meant for you. We’ll provide a step-by-step solution on how you can make any file executable, either using the CLI or the GUI method.

Let’s take a look at the steps.

Method 1: Using The Command Terminal

The first method makes use of the Command Terminal. You can make any file executable by typing certain commands in the Terminal.

Although we’re going to be using Ubuntu 20.04 LTS in this guide, the steps shouldn’t be any different for other Linux distributions. With that said, just follow the steps explained with the help of an example.

First and foremost, open the Command Terminal on your system. The shortcut for Ubuntu is Ctrl + Alt + T.

For this guide, we’ll create a sample file using the echo command by using the following command:

This command will create a string literal with the text “This is a guide on how to make a file executable in Linux” and store it in a file called Test1.

To view the content of the file, type the following command:

You should notice that the file doesn’t end with an extension name. This means that you have the option to make the file executable.

In order to make Test1 an executable file, we’ll use the following command:

In case of a file extension, the command becomes:

Now you can call your file by typing its name in the Terminal as follow:

You can also execute the command in the following manner:

This will provide appropriate permission for the file to be executed.

If the CLI method seems complicated to you, worry not as you can achieve the same results with the help of the GUI.

Method 2: Using the GUI

Unlike the CLI method, the GUI method is much less daunting and simplified to understand what’s going on.

Follow these steps to make a file executable using the GUI.

Читайте также:  Build linux app on windows

Start by navigating to the file of your choice. Once you’ve navigated to the file, right-click on it and select “Properties”. A new window should appear.

Once a window opens, click on the Permissions tab.

In the Permissions tab, you should see an option titled “Allow executing file as program.”

You should now have the desired file in an executable format if you followed the steps correctly.

Understanding How File Execution Works

Learning how file execution works in Linux has its benefits as it provides more flexibility when it comes to an understanding of how the file works.

In Method 1, we used the command chmod +x. This was necessary in order to make the file executable since the file required “read” privileges. The “./” tells the Terminal to search for the location of the file.

Aside from Method 1, there are other ways to use the $ chmod command. This flexibility makes $ chmod extremely valuable. A list of options for the $ chmod command is given below:

  • $ chmod 775 . This mode allows anyone to execute the file. However, only the owner of the file has permission to write in that file.
  • $ chmod 0010 . Only users of a group will be allowed to execute the file.
  • $ chmod 0100 . Permission to execute the file belongs exclusively to the user.
  • $ chmod 777 . Provides permission to execute the file to all Linux users.
  • $ chmod -777 . Doesn’t allow any user to execute the file.

Additional Information

Although the $ chmod command works for files without extension type, it should be noted that you’ll need to specify the file type in case it’s mentioned. For example, if you are dealing with a file that has a .run or .bin extension. The syntax for the execution command would be:

Additionally, make sure you have the correct name, file type, and file location before making any file executable.

Conclusion

If you followed the steps in the guide correctly, then the good news is you are now aware of how to make a file executable in Linux.

This guide covered different methods to make the file executable in Linux. We started by explaining the method involving the Command Terminal and followed up with the method to achieve the same with the help of the Graphical User Interface(GUI). We also covered additional uses of the $chmod command and the different permissions.

With this, we wish you all the best in your journey to master Linux.

Источник

Оцените статью
Adblock
detector