Linux execute file in terminal

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can’t find any option similar to Right ClickOpen WithOther Application.Terminal. How do I open this file in the terminal?

You shouldn’t use extensions on scripts. At some point in the future, you may find that a different language is more suitable to do the task your current script is doing. And then you have a problem. Do you keep the old name, with a completely misleading extension, or do you rename it, possibly having to edit alot of places where your script is used?

You don’t need the file extension. It’s nice to have but is not needed. The OS doesn’t look at the file extension. It looks at the data

16 Answers 16

Give execute permission to your script:

chmod +x /path/to/yourscript.sh 

Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:

Actually, you can use . /path/to/yourscript.sh if the script have to set up some environment variables.

Nobody mentions the traditional: ./path/to/yourscript.sh (without the space after . )? I find that one is the simplest and easiest to use. But anyways, here is my alternative that should do almost the same as ./ would, though I don’t see why you wouldn’t use ./ : (FILENAME=~/rem4space.sh;SLL=$(cat $FILENAME|head -1|sed ‘s:^#!\(.*\):\1:g’);[ ! -z $SLL ] && exec $SLL $FILENAME;sh $FILENAME) . edit FILENAME to your liking. Also note that sh will be used if there is no alternative.

You need to mark shell scripts as executable to run them from the file manager:

  1. Right click on your .sh file and select Properties: enter image description here
  2. In the Permissions tab, check Allow executing file as program: enter image description here
  3. Close the Properties window and double-click the file. A dialog will pop up giving you the option to run the script in a terminal: enter image description here

This isn’t working in Ubuntu 13.04. Keeps opening in gedit anyway, never asks me to execute. Edit: Nvm, imjustmatthew answers this.

Before using this we need to make the file permission for execute using chmod. chmod +x filename.sh or chmod 755 filename.sh

Источник

How to Execute .RUN Files in Linux

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time.

This article has been viewed 54,662 times.

.RUN files usually contain program data and installation instructions for Linux programs. This wikiHow teaches you how to execute .RUN files in Linux using the Ubuntu terminal. Since RUN files install software, make sure it’s legitimate and won’t harm your computer before executing it. RUN files from suspicious links can contain malware and damage your computer.

Image titled Execute .RUN Files in Linux Step 1

Press Ctrl + Alt + T to open a Terminal window and navigate to the folder where your .RUN file is. You can also search for Terminal in the «dash» bar on the left side of your screen by clicking the «All Applications» icon.

Читайте также:  Run all platform windows linux mac os x unix ios android

Image titled Execute .RUN Files in Linux Step 2

Image titled Execute .RUN Files in Linux Step 3

  • If you get a «Permission denied» error during this process, add » sudo » to the beginning of the code so it will run with the appropriate permissions. [1] X Research source

Expert Q&A

Tips

You Might Also Like

Install Google Chrome Using Terminal on Linux

Can Linux Run Exe

Can Linux Run .exe Files? How to Run Windows Software on Linux

Add or Change the Default Gateway in Linux

Open Ports in Linux Server Firewall

How to Open Linux Firewall Ports: Ubuntu, Debian, & More

Become Root in Linux

Take a Screenshot in Linux

Execute INSTALL.sh Files in Linux Using Terminal

How to Run an INSTALL.sh Script on Linux in 4 Easy Steps

Ping in Linux

Use Ping in Linux: Tutorial, Examples, & Interpreting Results

Boot Linux from a USB on Windows 10

Delete Read Only Files in Linux

How to Delete Read-Only Files in Linux

Use Wine on Linux

Run Files in Linux

Install Linux

How to Install Linux on Your Computer

Install Puppy Linux

How to Install Puppy Linux

Источник

How to Make a File Executable in Linux terminal?

New to Linux command line and wondering how to make a bash script or some other file executable? Here’s how to do it.

Each file that is in a POSIX-compatible file system (Ext4, Btrfs, XFS, JFS etc) has «mode bits» assigned to itself.

To make a file executable in Linux, the executable mode bit needs to be enabled.

To set the executable mode bit, the chmod command is used like this:

With that, you can execute said file from the terminal:

That was the quick summary. Let’s see things a bit in detail.

Make a file executable in Linux

There are several ways you can make a file executable in Linux. The most common methods involve using the chmod command, in different ways.

To check if you can execute a file, use the -l flag with ls command.

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

In the first column, -rw-r—r— represents that the owner can read and write, users from the group can only read and everyone else can only read the file.

Note the lack of x from the permission symbols. This means that lhb.txt is not executable.

I highly recommend reading about Linux file permissions and brushing up your basics to better understand these commands.

Method 1: Make file executable for everyone

The first method, and the most straightforward one, is to make a file executable using the following command:

The x flag is to set or unset the executable permission of a file. And using + sign before x means we want to set it as an executable file.

This will make the file executable for the owner, group and everyone else. Anyone on the system, will be able to execute it as well.

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod +x lhb.txt $ ls -l lhb.txt -rwxr-xr-x 1 pratham staff 0 Mar 10 20:49 lhb.txt

As you can see now, everyone, the owner, group and others have the executable x bit set. This means everyone can execute this file now.

Method 2: Make file executable only for certain user or group

If you do not want to make everyone able to execute a file, you should specify classes before adding/removing file permissions.

The available classes are:

  • u : permissions for owner/user
  • g : permissions for group
  • o : permissions for others
  • a : permissions for everyone

When you specify a class, the syntax is mostly similar to the one you saw in the first method.

This means, if I want to make the file executable in a manner such that only the owner of the file can execute it, and no one else, then I should use the following command:

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod u+x lhb.txt $ ls -l lhb.txt -rwxr--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

Similarly, to make it executable for everyone in the group owning that file, I should use the following command:

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod g+x lhb.txt $ ls -l lhb.txt -rw-r-xr-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

When you specify the a class, it is almost as if you did not specify any. Let’s have a look.

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod +x lhb.txt $ ls -l lhb.txt -rwxr-xr-x 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod 644 lhb.txt #reset permissions $ chmod a+x lhb.txt $ ls -l lhb.txt -rwxr-xr-x 1 pratham staff 0 Mar 10 20:49 lhb.txt

These commands might appear differently, but they have the same outcome.

Читайте также:  Linux монтируем сетевой диск

Method 3: Use the octal numbers

If you follow the first method, you only enable the executable flag of a file by doing a +x , making it such that anyone can execute it.

But, sometimes you might not want everyone to execute a file, maybe it should need some privileges. In that case, you can specify the full set of permissions.

That is usually done by either providing an octal value or a symbolic value to enable file permissions.

This allows you to have a more granular control over how the flags are set. Below is the syntax for using octal values with chmod command:

If you are not good with numbers, you can use an online chmod calculator, which helps you generate correct octal values and thus avoid using the wrong value and messing up file permissions.

Say, I want to make the file executable only for the owner and group, I need to know its current permissions first, and add the executable bits to it on top of it using [chmod-calculator].

$ ls -l lhb.txt -rw-r--r-- 1 pratham staff 0 Mar 10 20:49 lhb.txt $ chmod 754 lhb.txt $ ls -l lhb.txt -rwxr-xr-- 1 pratham staff 0 Mar 10 20:49 lhb.txt

As you can see, only the owner and group can execute the file.

Conclusion

This article covers making a file executable in a Linux system. It mainly focused on the different ways to use the chmod command. If you want to know more about it, we have a few examples on how to use the chmod command.

Источник

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.

Читайте также:  Поменять ip адрес компьютера linux

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!

Источник

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