Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
Refer this answer. –  green7 Jan 16 at 7:37

4 Answers 4

up vote 8 down vote accepted

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 with every new terminal-session will have the $PATH variable expanded with your directory.

share|improve this answer
    
nice idea @MadMike :) But don't you think so that /usr/local/bin is meant for exhibiting same idea ?? I am asking this because I am new to Linux and not exactly sure about your idea of "~/bin" and "/usr/loca/bin". –  v1h5 Jan 16 at 11:44
    
The difference is with the multi user aspect. "/usr/local/bin" is for everyone. "~/bin" is only meant for the current user. "/usr/local/bin" will need root rights. "~/bin" won't. –  MadMike Jan 16 at 12:27
5  
Just an observation: by default in Ubuntu ~/bin is already added to the $PATH even if ~/bin is not already created. You can check ~/.profile file to see this. –  Radu Rădeanu Jan 16 at 12:50
    
Going to add this to my answer as soon as I get to a Ubuntu machine. –  MadMike Jan 16 at 15:03

Before answering your question, let me explain "how it works" a little bit.

In your system you have Environment Variables. One of them PATH variable, that store set of directories where executable programs and scripts are located. In Linux, by default, PATH store a few directories. One of them /bin. That's why you can execute any program, which is located in /bin. You can check your PATH variable executing this:

echo $PATH

In PATH variable order of directories is important. The highest priority path names are specified first.

Now answering your questions:

  1. Yes it is safe to create symlink in /bin. Of course if you do not rewrite other executables. But you should prefer to use /usr/local/bin. It is also must be in your PATH.

  2. Yes it is. Depending on order in PATH. For example if you put symlinks of different files but with the same name both to /bin and /usr/bin, and /usr/bin included in PATH earlier than /bin, then it will use symlink from /usr/bin.

You can read about Filesystem Hierarchy here

About Ubuntu Environment Variables here

share|improve this answer
    
+1 for detailed explanation. Thanks c0rp.. :) –  v1h5 Jan 16 at 11:35
  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.

    There's a security risk. I really don't think it's wise to put your own symlinks in the /bin directory because not only your commands become global but also symbolic links can easily be broke. bash PATH environment variable is a more robust solution.

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

    No. Although according to the Filesystem Hierarchy Standard (FHS) there's a difference between /bin and /usr/bin.

    /bin Essential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp.
    /usr/bin Non-essential command binaries (not needed in single user mode); for all users.

    But the difference is not that important today. Actually there's a tendency in some distributions (esp. Fedora) to fade out /bin directory.

share|improve this answer

NO!!! Do not touch system directories. Every script or program you wrote you should normally place in /usr/local/bin and /usr/local/sbin directories. And adding directories to PATH is not good idea, because it has security risks.

Also, the difference between those directories that 's' s for superuser programs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.