55

I'd like to know if there is a way to run program/shell script without typing full path:

/opt/idea/bin/idea.sh
  • You can always give the full path of the file. – muru Sep 22 '14 at 8:32
  • Thank you @muru, this I know, but it is a program I use often, so I would like to be able to run it with a simple command, not a long file path. – Jonny Sep 22 '14 at 8:33
  • nufailm.blogspot.com/2012/05/… this will help for idea :) – dedunumax Oct 10 '14 at 5:17

10 Answers 10

56

You can just create symlink. Create it in /usr/local/bin. All you need is to run command:

sudo ln -s /full/path/to/your/file /usr/local/bin/name_of_new_command

After that you should make your file executable:

chmod +x /full/path/to/your/file

Now you should be able to run name_of_new_command at any time in your terminal.

Note that this is good solution only for home usage of Linux.

  • 10
    Note that you shouldn't place a symlink in /usr/local/bin that points to a script in a private home folder, such as /home/jack/myscript.sh, as only user jack would usually be able to execute it. The symlink will be visible for other users, but not the file it points to. On a single-user system this may not matter, but still, it is "good practice" to place scripts (or links to scripts) that all users can read in /usr/local/bin, and private, self-made scripts (or links to scripts) of a single user in their ~/bin folder. – Malte Skoruppa Sep 22 '14 at 11:27
  • add a symlink in /usr/sbin/ makes it accessible from anywhere – Behzad Seyfi Oct 13 '18 at 16:48
27

You can add /opt/idea/bin to your PATH variable:

PATH=/opt/idea/bin:"$PATH"

After this you can run it with simply idea.sh.

You probably want to add this line in your ~/.bashrc file.

  • 2
    also: if you make the script executable, the .sh extension can be left away (assuming you use the shebang at the top of the script). simply "idea" is enough then. – Jacob Vlijm Mar 1 '14 at 8:24
  • 1
    @Jacob: you cannot leave off the extension, as that is part of the executable's file name (this isn't Windows, where magic like that causes other issues). In order to call the script with the name idea, you would have to create a link or alias with that name pointing at the idea.sh script. – Jonathan Callen Mar 1 '14 at 13:25
  • 2
    @JonathanCallen Callen you certainly can! make the script executable, remove the .sh extension from both the filename and the command to run it, given the fact that you start the script with the shebang #!/bin/bash. – Jacob Vlijm Mar 1 '14 at 15:25
  • 1
    @Jacob what I mean is without renaming the file, you cannot (and it appears this is part of a larger software package, so you shouldn't just rename files) – Jonathan Callen Mar 1 '14 at 21:29
  • 2
    Thanks @Jacob, notice it says "When scripts are installed into a directory in the system PATH". The idea.sh script is not such script, in fact moving it outside its installation dir is completely unsupported. The Debian packaging rules don't apply here. – janos Mar 1 '14 at 21:55
14

You can create a function in your ~/.bashrc:

some-name () {
    /path/to/your/file
    # or:
    #cd /path/to/your
    #./path
}

Or you can create an alias:

alias some-name='/path/to/your/file'
# or  
#alias some-name='cd /path/to/your/; ./file'

In both cases, you can run it by calling:

$ some-name

If the file doesn't depend on where it's running, consider adding it to your ~/bin:

mkdir -p ~/bin
cp /path/to/you/file ~/bin
# or mv /path/to/you/file ~/bin
# or ln -s /path/to/you/file ~/bin

~/bin, if it exists, gets added to your $PATH automatically. Then you directly call file:

$ file

(Bad choice of name though, consider calling it something less generic.)

  • @Oli always give my the five minutes, I tend to add indents. :) It's just annoying as I need to trick the editor to use Ctrl-K for second-level indents. – muru Sep 22 '14 at 8:39
  • 2
    +1 in particular for the solution of adding file to your ~/bin, which is what I usually do (for longer scripts). I'd have probably even put that one at the top :) Aliases are good for shortcuts to commands with certain parameters, e.g., alias l='ls -CF'. Though I have seen much more awesome uses of aliases. I don't tend to declare functions in ~/.bashrc (I prefer to have all my scripts organised in separate files in ~/bin). Though all this is a matter of taste, I guess :) – Malte Skoruppa Sep 22 '14 at 9:19
  • @MalteSkoruppa I just gave preference to the methods that most easily lend themselves to cd, because the script may depend on the working directory. :) Otherwise, yes, I tend to put files in ~/bin myself. – muru Sep 22 '14 at 9:23
10

You can create a launcher by using following command:

gnome-desktop-item-edit --create-new <path-where-to-save>. I t will open this window.

enter image description here

Name it whatever you like and in command box type following

sh -c '/opt/idea/bin/idea.sh' and save it.

Now you can run that file using newly created launcher

OR

You can create a .desktop file with following contents

[Desktop Entry]
Name=<whatever-you-want>
Exec=sh -c '/opt/idea/bin/idea.sh'     
Terminal=false
Type=Application
Icon='<path to an icon file if you want>'

Now save it with .desktop extension on any place.

Make it executable with this command chmod a+x <your-desktop-file>

Now double click to open it.

7

We can define a function and a add hotkey by using bind command to call that. Open ~/.bashrc file and add these line to it:

# define function that opens your program in working directory
Openprog(){
    /your-Program/path/here
}

# bind hotkey to it (<F12>)
bind -x '"\e[24~":"Openprog"'

Now when you press F12, your program will launch.

Note: A quick way to determine the escape code:

Open your terminal and press Ctrl+V. Now press your favorite keyboard shortcut. The correct escape code should appear. Just make sure to replace ^[ with \e before adding the shortcut to, e.g. replace ^[[24~ with \e[24~.

7

We can also run /opt/idea/bin/idea.sh file directly using bash_aliases

Open ~/.bashrc file by running,

gedit ~/.bashrc

Remove the # before the lines and save it, so that the lines will look like,

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Now open ~/.bash_aliases file,

gedit ~/.bash_aliases

Add the below lines in that file and save it,

alias idea='cd /opt/idea/bin && sudo ./idea.sh'

Finally source the ~/.bashrc file,

source ~/.bashrc

Now you can run /opt/idea/bin/idea.sh file directly by,

idea
4

In addition to the other good answers, consider symlinking into ~/.local/bin and adding this directory to your PATH (from within your .bashrc for instance). This method does not require special permissions (unlike symlinking to /usr/local/bin, for instance). This way, you may have a standard directory struction without flooding your $HOME. Read more about this on these https://unix.stackexchange.com/ questions:

1

Create a soft link of it in /usr/bin direcotyr:

ln -s /usr/bin/idea.sh  /opt/idea/bin/idea.sh

Now run it using:

idea.sh
0

Or you can simply use

nano ~/.bashrc

and add

PATH=/full/path/to/file:"$PATH"

at the end of it, then save and exit. Afterwards, you can simply type the file name.

0

I followed all the answers here and other places and so few fail to mention that you may need to LOG OUT for the command to eventually work.

Just to recap, especially for Xubuntu, but for other Debian/Ubuntu variants as well I wrote these simple instructions.

(in the following instructions we use directory ~/bin, because that's automatically a place from where these OSs look for commands. See here:

Fool-proof instructions to get your command to work:

# Open Terminal with Ctrl + Alt + T (or your system's own shortcut)

# You'll work in your home folder, no need to use sudo for any of this

cd # Go to home directory

mkdir -p bin # Create folder ~/bin if it doesn't exist

# Be careful not to type /bin, that's the bin folder in system root: /

sudo apt install nano # Skip this if you have Nano installed

nano bin/yournewcommand

    # In Nano, type:

    printf "Your new command works! \n" # \n means line break

    # Ctrl+X To leave Nano

    # Ctrl+Y To save the unsaved file

    # Enter to confirm

chmod +x bin/yournewcommand

yournewcommand # If you just created the bin folder, this command might not yet work.

# You must now log out and log back in to make the bin folder noticed (I think)

yournewcommand # Now it works! (You can use Tab to autocomplete the command you're typing)

# If you add a second file/command, it should work without logging out (at least in my tests)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.