Bash - use your local bin

When browsing your server's file system, you may notice directories such as 'bin' and 'sbin'. These folders contain binaries and scripts that execute routines and, put simply, 'do stuff'.

Having a local 'bin' for your personal scripts is a great time saver and keeps all your executable files in one place.

As a system administrator, whether running a single VPS or a 100 fully fledged servers, one thing you should keep in mind is portability.

Having written a very nice backup script that you want to run every day is excellent - automation of tasks is very important - but if you're not sure where to put the script or where it would go on a different server, it can cause problems.

Like most good ideas, the solution is very simple: have a 'bin' in your home folder.

That way, any scripts you have written are in one place. They can be instantly copied to a new server and are instantly available at the command line.

Log into your VPS and in your home folder create a 'bin' directory:

mkdir ~/bin

It's likely that this folder will automatically be part of your 'PATH' (when a command is given, the 'PATH' is a list of locations that are searched for that command and should include all the 'bin' and 'sbin' locations on the server).

You can check your 'PATH' locations like this:

echo $PATH

The output on my test server includes:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin

There are more locations in the output but you get the idea. If your home directory 'bin' folder is not listed we need to add it.

To do this, open your .bash_profile in a text editor:

nano .bash_profile

Ensure the following is in your profile:

# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
    PATH=~/bin:"${PATH}"
fi

The code is fairly self explanatory - it checks that the 'bin' directory exists, and if it does, adds it to your 'PATH'.

If you did have to alter your .bash_profile you should log out and log back in again to ensure the additions are added to your 'PATH'.

This time, when I issue the 'echo $PATH' command, my new 'bin' folder has been added:

/home/paul/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin

Now I can add my scripts to this folder and have them instantly available from the command line.

As already mentioned, the huge advantage of this is that I can copy the folder to another machine and instantly have the scripts available there.

PickledOnion.

Digg it | del.icio.us | reddit | StumbleUpon

Article Comments:

dayo 30 Jun, 2007

when you say "home folder" do you mean /root?

PickledOnion 30 Jun, 2007

Hi dayo,

I mean the home folder of the main user. So for example, /home/paul/. The 'bin' directory would then be located at /home/paul/bin/.

It is the bash profile of that user that is edited to ensure the 'bin' is in the users path.

I hope that clarifies it.

PickledOnion.

dayo 30 Jun, 2007

Still confused lol. My VPS (CentOS) has the following path to the public folder "/var/www/vhosts/domain.com/httpdocs"

Would that be "vhosts" or domain.com in this case?

Cheers

PickledOnion 30 Jun, 2007

I think you are confusing the path(s) automatically searched by the user when a command is given and the directory your server will use for your domains.

Can I suggest you email me to see if I could help you?

My email is on the contact page.

PickledOnion

dayo 30 Jun, 2007

ok thanks

Comments are closed for this article.