Creating a program in bin

There are two ways of allowing you to run the binary without specifying its path (not including creating aliases or shell functions to execute it with an absolute path for you):

  1. Copy it to a directory that is in your $PATH.
  2. Add the directory where it is to your $PATH.

To copy the file to a directory in your path, for example /usr/local/bin (where locally managed software should go), you must have superuser privileges, which usually means using sudo:

$ sudo cp -i mybinary /usr/local/bin

Care must be taken not to overwrite any existing files in the target directory (this is why I added -i here).

To add a directory to your $PATH, add a line in your ~/.bashrc file (if you're using bash):


... if the binary is in $HOME/bin.

This has the advantage that you don't need to have superuser privileges or change/add anything in the base system on your machine. You just need to move the binary into the bin directory of your home directory.

Note, changes to .bashrc takes effect when the file is sourced next time, which happens if you open a new terminal or log out and in again, or run source ~/.bashrc manually.

You should read more about the PATH variable. I strongly suspect that you should use (or change) it appropriately.

I want to be able to put it into the bin directory

I guess you mean /bin/ or /usr/bin/. I am not sure it is wise to add your own programs there. Perhaps it would be better to put a symlink inside them (e.g. using something like ln -sv $(realpath foobar.out) /usr/bin/), if you absolutely want your program to appear there (which IMHO is probably a mistake since you are messing up with your distribution's package manager).

Probably installing your program in /usr/local/bin/ is much wiser (to leave both /bin/ and /usr/bin/ intact, as provided and managed by your Linux distribution). See LSB and read more about the File Hierarchy Standard.

... like system wide commands e.g .... cd

This is a strong misconception of your part (in the original variant of your question, you rightly removed cd later). cd is not and cannot be an executable, it is (and has to be) a builtin command of your shell.

You may want to read the freely downloadable Advanced Linux Programming to understand why cd cannot be an executable (the current working directory is part of the state of each process, and every process has its own one and could change it using chdir(2)...)

install /path/to/oobar.out /usr/local/bin/