I've compiled linked and created a program in C++ now I have
I want to be able to put it into the bin directory and use it like system wide commands e.g.
ssh, echo, bash, cd...
How can I achieve that?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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):
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 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
To add a directory to your
$PATH, add a line in your
~/.bashrc file (if you're using
... if the binary is in
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
/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
/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 ....
This is a strong misconception of your part (in the original variant of your question, you rightly removed
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)...)