I'd like to know if there is a way to run program/shell script without typing full path:
/opt/idea/bin/idea.sh
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI'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 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.
/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.
Sep 22, 2014 at 11: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.
idea
, you would have to create a link or alias with that name pointing at the idea.sh
script.
Mar 1, 2014 at 13:25
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.
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.)
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 :)
Sep 22, 2014 at 9:19
cd
, because the script may depend on the working directory. :) Otherwise, yes, I tend to put files in ~/bin
myself.
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.
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.
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~
.
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
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:
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
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.
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:
# 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)