I know that the linux commands like ls, cd and others are executables and can be accessed from any directory. My question is how can i run an executable created by me from any directory in terminal ?
Read about the PATH variable. It could be set by your shell. Check with echo $PATH its current value. It is also used by several exec(3) functions. BTW, having a long $PATH is bad taste and messy (and could be inefficient).
If your login shell is /bin/bash you could edit your ~/.bashrc (used for interactive shells) to add something like
PATH="$PATH:/something/more"
but on several recent Linux distributions, the $HOME/bin/ directory is already part of your PATH, and you might add scripts, executables, or symlinks to them in it.
So (when $HOME/bin is mentioned in $PATH) I don't recommend extending your PATH, but rather adding appropriate executables, executable scripts or symlinks into that $HOME/bin/ directory.
Of course, if you have some executable in $HOME/someproject/someprog you can still explicitly run it with a shell command starting with $HOME/someproject/someprog.
Your build procedure might also have some installation step. For example, if you use GNU make as your build automation, you might have an install phony target in your Makefile which copies the executable after its compilation into some appropriate place. See also hier(7) & install(1), and autoconf.
Look for inspiration into the source code of some existing free software, e.g. on github.
Notice that many utilities (e.g. cron and your crontab(5)) don't use your interactive PATH (but some reduced default one). So you might want to give an absolute path of some script when using crontab(1).
The directory that the executable is located in must be available in your $PATH. You change the path by modifying your shell's startup files (typically ~/.bash_profile or ~/.bashrc if you're using bash), adding
PATH="$PATH:/some/directory"
... where /some/directory is the path to the directory where your executables are located.
The change will take effect when you open a new (login) shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With