Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Beginner at linux here, is there a way to create a permanent link between a command and the directory it's within. I've got XAMPP installed and to start i'm using:

/opt/lampp/lampp start

Is there a way to change this so instead I can just type

lampp start

Without changing the directory (i.e like how normal commands are executed in linux)

share|improve this question

migrated from stackoverflow.com Nov 7 '10 at 1:52

This question came from our site for professional and enthusiast programmers.

6 Answers 6

Assuming you're using Bash.

The way you would do this is by adding the directory /opt/lampp to your PATH variable:

export PATH=${PATH}:/opt/lampp/

To retain this change permanently, you could add the above line to your .bash_profile file. As demonstrated already, there are multiple answers to this kind of problem. I'd recommend dedicating a bit of time to reading about Linux in general, it will save you time in the long run. A decent Bash tutorial is here: http://www.ibm.com/developerworks/library/l-bash.html

share|improve this answer
1  
Just out of curiosity, I've always set my PATH with PATH=$PATH:/whatever/dir instead of PATH=${PATH}:/whatever/dir. Is there any difference between using the curly brackets and not? –  Wuffers Nov 7 '10 at 2:00
    
There's actually a lot you can do with curly braces in Bash. When referring to variables, I use them to delimit the variable contents from the string it's being attached to. Your example works because of the : and the / in between PATH and the rest of the content. If (for example) you tried to join the string like: PATH=$PATHwhatever/dir, you'd run into problems. For other cool stuff you can do with curly braces, search for "curly brace expansion" or play around with tricks like: ${COLOUR:-blue} –  Paul Andrew Nov 8 '10 at 15:54

You can also create an alias. Edit the file ~/.bashrc and add a new alias, such as :

alias startlamp='/opt/lampp/lampp start'

Then, you can type startlamp instead of /opt/lampp/lampp. Please note that this alias is only valid for your own user, and not everyone that uses your computer.

This does not create a link, though, unlike Travis' command.

share|improve this answer

Sure is. Try this:

ln -s /opt/lampp/lampp /usr/bin/lampp

ln -s creates a symbolic link to /opt/lampp/lampp from /usr/bin/lampp. The reason that we put it in /usr/bin is because that is where all your standard UNIX commands will be found, such as "ls" and "rm".

Now you can type lampp start no matter what directory you are in!

Note: you will probably need to run the above command as superuser, or root.

share|improve this answer
    
Brilliant works perfectly, thanks to all other answers as well. Thank you. –  akd5446 Nov 6 '10 at 18:10

Basically you'd want to either make a symlink (as Travis points out) point to the app from a directory which is within your path, or add /opt/lampp to your path. If you want it for your account only, you can put it into your shell's rc file in your home directory (~/.bashrc, for instance), or in the system shell config (/etc/bash.bashrc) which would make it apply to everyone.

share|improve this answer

I suggest making a bash alias.

Login with a non root account and open a terminal window
After you get the terminal window open, use vi to open ~/.bashrc

vi ~/.bashrc

In vi, either look for the aliases or just go to the end of the file
Press the 'i' key (without quotes) to enter insert mode
Type the following into your .bashrc file

alias lampstart='/opt/lampp/lampp start'

Press ESCAPE to exit insert mode Type the following to save the .bashrc file and quit vi

:wq!

At the terminal, type the following command to reload your .bashrc file

bash

now type lampstart in your terminal, and lamp should start. You could actually name the alias whatever you want, i just used the alias lampstart because it seemed logical.

share|improve this answer

I like to put all my custom commands in a ~/bin folder which I add to my path.

In ~/bin/lampp, I would have:

#!/bin/bash

/opt/lampp/lampp start

Make sure you chmod a+x ~/bin/lampp. Then you can run lampp anywhere and will have the desired behavior.

The advantage of this method is you have a central location for all your own commands and aliases.

CORRECTION:

This works better for more complex commands. For more basic commands, a symlink instead of a script is better, as in Travis' answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.