73

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

/opt/idea/bin/idea.sh

10 Answers10

81

You can just create a symlink in /usr/local/bin. All you need is to run this 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 a good solution only for home usage of Linux.

terdon
  • 104,119
c0rp
  • 10,010
33

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.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
janos
  • 4,968
17

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.)

muru
  • 207,228
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.

g_p
  • 19,034
  • 6
  • 59
  • 69
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
αғsнιη
  • 36,350
Avinash Raj
  • 80,446
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~.

αғsнιη
  • 36,350
5

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:

abstrus
  • 151
  • 1
  • 2
1

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

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

Now run it using:

idea.sh
J.Franks
  • 326
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)