Как установить команду без определения пути каждый раз?

303
lvthillo

У меня есть команда, которая:

./keytool 

Но я всегда должен идти в эту папку, чтобы использовать ее:

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin 

Как я могу просто использовать следующую команду во всей моей среде вместо определения пути каждый раз?

keytool 

Как мне это сделать?

1
Возможный дубликат [Как установить псевдоним постоянно с помощью команды?] (Http://superuser.com/questions/205714/how-to-set-alias-permanently-using-command) Francisco Tapia 8 лет назад 0

2 ответа на вопрос

1
MetalGodwin

One way would be to create a symbolic link to any folder currently in your PATH-variable, in this example we use /usr/bin/.

sudo ln -s /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin/keytool /usr/bin/keytool 
1
francesco foresti

Add the following line at the end of the .bashrc (or .profile) file that is in your home directory :

export PATH=$PATH:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin 

This is however a very fragile approach, since it will not work as expected as soon as you update your java installation. If you want to do it more properly, I suggest a couple of ways:

look in the /etc/alternatives folder, see if there's a link named javapointing to the current java home (could be pointing to another link). If it's there, change the export line with

export PATH=$PATH:/etc/alternatives/java 

or (depending on your system), you could have a link in /usr/lib/jvm, something like /usr/lib/jvm/java pointing to the most recent version of the jvm you have installed. If it's there, you can use this one, so the export line is

export PATH=$PATH:/usr/lib/jvm/java 

Apart from that, if you want it to be system-wide (instead of limiting the path extension to your user), you should add the line to /etc/bashrc (or /etc/bash-bashrc if it's there).

As a final note, this will only work in newly-created shells, not the ones you already have, unless you issue the command

. ~/.bashrc 

(or the file you've modified, if it's not .bashrc) in those shells.