Thursday, May 2, 2024
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 14979  / 3 Years ago, thu, august 12, 2021, 1:26:52

I have many command line tools that I use frequently.
I was using the normal method to traverse till the complete path of that command through terminal and then did ./command-name BUT this is a cumbersome and time-consuming task.



Researching the topic of "How to execute a command from anywhere through terminal", I ended up in solution to include the path in the environment variable (by editing ./bashrc).



This works fine.



I even found out that all the commands that work globally(like mkdir, ifconfig, cp, etc ) are placed in "/bin" directory.



On including the "symlinks" of my commands in "/bin" directory, all is working fine and I can execute the commands from anywhere in terminal.



My Question




  1. Is it safe to place symlinks in /bin directory directly. I am asking this because while placing the symlinks, it asked me for administrator password.


  2. Will it create any difference if I place commands' symlinks in /bin as compared to /usr/bin directory ?




Environment (if it matters):



Ubuntu 13.10, 32-bit.


More From » command-line

 Answers
2

It isn't recommended to mess with /bin and with /usr/bin even though this will work it is somewhat unsafe. Also, you don't need to place your symlinks there.



I've had the same problem like you mentioned. It's cumbersome to navigate to your scripts and call them like ./myscript each and every time. Here is what I did.



Define your own bin-directory



You can create your own bin-directory and put your scripts there. Create it with



mkdir ~/bin


Now you will need to tamper with the PATH environment variable. This is a sensitive area. If you mess up the PATH variable you won't be able to execute commands with relative paths anymore. If that happens ls might not work any more /bin/ls will still do.



What I'm going to show right now will only affect the current running terminal session. So if anything goes wrong you simply log out with Ctrl+D and everything is like it was before.



echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
export PATH=$PATH:~/bin
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/madmike/bin


The echo-command shows what's in $PATH before and after. export allows you to change the environment variable. Your additional bin-directory is appended to the end of the variable. This means your commands have the least priority should there be more than one command with the same name. This is good, since you don't want to override essential system commands.



To make the changes permanent, edit your ~/.bashrc file.



nano ~/.bashrc


add export PATH=$PATH:~/bin to the end of that file.



Now every new terminal-session will have the $PATH variable expanded with your directory.


[#27444] Friday, August 13, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
afisird

Total Points: 193
Total Questions: 112
Total Answers: 111

Location: Angola
Member since Mon, Jul 12, 2021
3 Years ago
afisird questions
Wed, Jul 27, 22, 03:53, 2 Years ago
Sun, Mar 12, 23, 18:05, 1 Year ago
Sun, Dec 11, 22, 01:19, 1 Year ago
;