I've compiled linked and created a program in C++ now I have
foobar.out
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?
I've compiled linked and created a program in C++ now I have
foobar.out
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?
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):
$PATH
.$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
):
PATH="$HOME/bin:$PATH"
... 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.
install
is nice; it makes the script root-owned. (So in theory, someone that cracks your account couldn't mess it up. At least not until they get root privs next time you manually sudo or something...)
– Peter Cordes
Sep 4 '17 at 7:49
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)...)
Copy your executable to /usr/bin/
, you should then be able to execute it like echo
or any other executable in /usr/bin/
cp -a <filename> /usr/bin/
chmod a+x /usr/bin/filename
I would add to the accepted answer by @Kusalananda Another advantage to placing your home brewed scripts and programs in $HOME/bin is when you upgrade your system, you won't lose them. It's a better way to manage your personal toolbox, and makes it more portable.
/usr/local/bin
is a good choice too, depending on whether it's a script that only you would use or not. I keep (parts of) my /usr/local on new installs.
– Peter Cordes
Sep 4 '17 at 7:47
foobar
, notfoobar.out
. (e.g.gcc -Wall -O3 -march=native foobar.c -o foobar
).a.out
is the default name, but don't take that as an example. – Peter Cordes Sep 4 '17 at 7:51