93

What should I do if I want to be able to run a given program regardless of my current directory? Should I create a symbolic link to the program in the /bin folder?

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
aneuryzm
  • 1,925
  • 5
    no, you should use the shell's PATH variable. – msw Nov 06 '10 at 12:28
  • 9
    I think it would be fine to create a personal ~/bin/ folder, add THAT to your path, and create symlinks in that folder to anything you wanted to be executable from anywhere. – frabjous Nov 06 '10 at 20:16
  • 2
    You can also set a alias with the full path to the binary you want to execute. – datasmurf Oct 14 '15 at 20:48
  • I came across this which might help if you want to place an icon on your desktop to do it

    https://www.unixmen.com/create-a-launcher-in-ubuntu-using-bash/

    – JonoJames Feb 28 '21 at 00:30

5 Answers5

70

If you just type export PATH=$PATH:</path/to/file> at the command line it will only last for the length of the session.

If you want to change it permanently add export PATH=$PATH:</path/to/file> to your ~/.bashrc file (just at the end is fine).

Flare Cat
  • 165
oadams
  • 2,335
27

The short answer is that to run the program, no matter what your directory, you need to have the program's directory in your search path. The problem can be solved by putting the program into a folder thats already in that path, or by adding a new folder to the path - either will work. The best answer depends on:

Is this program a downloaded program that you have compiled yourself from source?

Its quite likely will have an install mechanism already. In the folder that you compiled the program, as root, run 'make install'

Is this program a downloaded program that you want to make available as part of the standard programs on the computer?

Makes sense to put this kind of application into a standard folder. its quite common to use directories such as /usr/local/bin for such programs. You will need root access to do this.

This is a program that you have written for yourself and/or you have no special privilages on the computer.

Creating a folder in your home directory called 'bin', and placing the program in there. You may need to edit your login script to add the full path to this folder (e.g. /usr/home/jeremy/bin)

Whilst you could just add its current directory to the search path, you will have to keep doing this with every new program - and is more work in the longer term.

23

Placing a link to the file in the /bin directory isn't the best thing to do for multiple reasons.

  • If the actual executable file is in a location that some users can't see or execute, they see it as a bad link or dysfunctional program.
  • The /bin directory is supposed to be reserved for programs which are required for running the system (things like chmod, mkdir, etc).

You can actually place (install) the executable file in /usr/bin/ or even /usr/local/bin/. Of course, you've manually installed the program at that point; your distribution isn't going to keep track of it the way it does the rest of your programs - you'll have to manually upgrade it when necessary and manually remove it if you want it gone. Also, you'll have to know what packages it depends on (it sounds like you already use the program, so that's taken care of, but in general...).

Unless I'm setting up a program that I expect other users to use, that's not what I usually do: I create a bin directory just for me in my home directory, and I edit my shell profile to add ~/bin/ to my PATH environment variable. I find it easier to keep track of the programs I've installed that way, because it is separated from the rest of the system.

xenoterracide
  • 59,188
  • 74
  • 187
  • 252
Shawn J. Goff
  • 46,081
  • placing it in /usr/local/bin and then restarting the shell worked perfectly for me. even placing it into /bin directly is good since i am the only user of this computer anyway. I don't really like the way of the PATH variable, coming from windows I kind of expect programs to be in a Programs folder, or system32 for that matter. also i wrote the shellscript myself, so after testing i'm comfortable of it lying in /bin. – clockw0rk Oct 06 '23 at 03:18
6

If you want to run a command foo in the directory your shell is currently in, you basically have two options:

  1. Type ./foo at the shell prompt.

  2. Add the . directory (. is a name for "the current directory") to the PATH environment variable; how you do this depends on the shell you are using:

    • for Bourne-type shells (bash, zsh, ksh, etc.) you write (see this page for more information):

      export PATH=$PATH:.
      
    • for csh-type shells (tcsh, csh) you write (see this page for more information):

      set path = ( $path .)
      

Note that 2. is a security risk on multi-user systems: imagine you cd to directory /tmp and a malicious user has created a malware binary named ls in there..

  • 2
    yeah, i think options show you that unix let's you do things, that is considered bad practise. Adding . to your path can also create other side effects that are generally bad. – The Unix Janitor Nov 06 '10 at 13:58
1

Messing with the PATH opens you up to running malicious programs by mistake. Standard is to install "local" stuff under /usr/local (see FHS, which codifies Unix usage: a bit dated but it gives rationale)., /usr/local/bin is usually in the PATH for all users.

My "locally installed" programs are in $HOME/bin, set in the standard dance by ./configure --prefix=$HOME ..., coupled with a few (bash) aliases to pick out e.g. self-built git over the system one (non-clashing ones are found in the PATH at the very end.

vonbrand
  • 18,253