Most operating systems are similar in the following respect. When it receives the command to run an executable, the system will try and run the file from the local directory first. For example..
Here, whilst logged into the root directory of the C: drive, the user attempts to run the DOS Editor executable, edit.com
. Will it work? Well that depends.
The system will first look into the root of C: (where the user is currently logged in the file system) and will attempt to find and run the executable. However it won't find it because edit.com
lives elsewhere, in the C:\DOS
directory. And the user is not currently logged into that directory.
What happens next is a bit nifty, a bit of a trick if you like. Because next, the system will look into the system path
to see if the file is there instead. What's the system path? Well it's basically a list of directories which the operating system sets up (although it can be added to later) at boot time and which resides in system RAM. How it works is, the operating system has a rule which says, "If you can't find the relevant file in the currently logged directory, look and see if it's in one of the directories in the path statement."
So what does this path statement look like? Well most of the time you can type path
and all is revealed. Once more under MS-DOS..
Here we can see that the path statement points to two places, the root of C: -> C:\ and the DOS directory -> C:\DOS. This means that if a user tried to run an executable whilst logged into any directory other than the above, the system would first search the currently logged directory and would then attempt to find the executable file in the directories specified in the path statement.
The great thing about a path statement is that you can run executables from anywhere in the directory tree, without having to change to the containing directory first or type the full directory path to the file.
So consequently the answer to the above is yes, it will work, provided the path statement points to the correct container for the file which you're trying to run. And yes, edit.com does live in the C:\DOS directory, which means as C:\DOS is in the path statement, you can run edit.com from anywhere. Phew!
The Unix Path
So how about Unix then? Well the situation's similar but not identical. FreeBSD certainly does have a path statement. Whereas under DOS (and Windows) all you have to do is type path, FreeBSD requires echo $PATH and note the case sensitivity.
When you log in to a Unix system, you're using one of the various shells available, such as csh and bash. Your PATH environment variable is inherited from the shell you are using at the time.
Here's an example, logging in to a FreeBSD 5.2.1 box under the bash shell.
su-2.05b# echo $PATH
I've chucked the path statement across two lines here as it's so enormously long. Note that different Unix directories in the path statement are separated by colons, whereas DOS and Windows separate them with semi-colons.
So why don't we just chuck every directory in the path ststament and be done with it - then we'd never have to type a path to anything!? Well this is true and it's the first thing I thought of when I first learned about paths. Different systems limit the size of the path statement, is the main reason. It would be unfeasably huge and unwieldly, is a second. It would use up extra RAM, is third. So the usual thing is to chuck the operating system directory in there, plus a few other crucial and commonly used directories. But there's room to sneak in a few of your own, if you know what I mean..
Using Symbolic Links to Save Your Fingers
And so we finally get to the subject of using Symlinks under Unix. The concept is as explained above. The trick is to use the Unix ln command to lob a copy of the executable file into a directory which is already in the path statement. See the link at the bottom of the page for the exact standards.
This is a fairly common thing to do, especially for often-used apps and most people pick on the /usr/local/sbin directory, which is in most system and shell paths. Here are some examples..
# ln -s /usr/local/apache/bin/apachectl /usr/local/sbin/apachectl
# ln -s /usr/local/mysql/bin/mysql /usr/local/sbin/mysql
# ln -s /usr/local/mysql/bin/mysqladmin /usr/local/sbin/mysqladmin
# ln -s /usr/local/mysql/bin/mysqld_safe /usr/local/bin/mysqld_safe
So, commonly used executable files for commonly used applications or daemons
, such as the Apache executable, apachectl
and the MySQL daemons, mysql
. Once the symlink is established, you can run the executable from anywhere in the tree without specifying the path.