Making your own custom command for a script file which can be executed from any user.
Here is a script file(/home/anuj/myscript/myscript.sh) which echo’s current directory on the terminal.
[java]echo $PWD[/java]
2. Make a symbolic link mycommand pointing to myscript.sh in /usr/bin or /sbin/ or /usr/sbin directory (Differences between them can referred from here).
[java]
cd /sbinln -s /home/anuj/myscript/myscript.sh mycommand
[/java]
Now you can use command “which” to see which file will executed if we type the command named “mycommand”
[java]
which mycommand
Output: /sbin/mycommand
anuj@intelligrape:/tmp$ mycommand
Output: /tmp/
[/java]
Its done! 🙂 Next question would be where we can use it to make our life easier??. Well ! Generally we have deployment script for that we need to switch to user and then we need to run the script file instead of doing that we can make a softlink of the script file and put it in the /sbin or /usr/sbin etc. Like some software packages like apache put softlink to there script file in /sbin.e.g. a2ensite is a softlink to a2enmode in /usr/sbin which is a executable script file.
There can be some better way to achieve the same so suggestions are most welcomed.
Hope this helps you guys !
Anuj Aneja
Intelligrape Software
Thanks Bhagwat Ji for sharing this nice way!.
Nice Blog.
However creating soft links for too many commands will be irritating instead I would like to move all the custom script files that are supposed to be globally available in a folder and somewhat change the PATH environment variable to include that folder in command search path.
There are few files like /etc/profile, /etc/bash.bashrc etc. which are executed for each new shell(any user). We can set the PATH variable here so that it will be included in all the user’s PATH variable.
I appended the following at the end of /etc/bash.bashrc file (sudo vim /etc/bash.bashrc) :
PATH=$PATH:/home/bhagwat/globalScripts
After then it looks for the scripts in above mentioned directory as well.