I have six directories with command files. These are /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin and /usr/local/sbin.

What are the differences between these? If I'm writing my own scripts, where should I add them?


Related:

up vote 301 down vote accepted

Please refer to the Filesystem Hierarchy Standard (FHS) for Linux for this.

  • /bin : For binaries usable before the /usr partition is mounted. This is used for trivial binaries used in the very early boot stage or ones that you need to have available in booting single-user mode. Think of binaries like cat, ls, etc.

  • /sbin : Same, but for binaries with superuser (root) privileges required.

  • /usr/bin : Same as first, but for general system-wide binaries.

  • /usr/sbin : Same as above, but for binaries with superuser (root) privileges required.


if I'm writing my own scripts, where should I add these?

None of the above. You should use /usr/local/bin or /usr/local/sbin for system-wide available scripts. The local path means it's not managed by the system packages (this is an error for Debian/Ubuntu packages).

For user-scoped scripts, use ~/bin (a personal bin folder in your home directory).

The FHS says for /usr/local:

Tertiary hierarchy for local data, specific to this host. Typically has further subdirectories, e.g., bin/, lib/, share/.

  • so /bin is where all the core files are installed. like ls, cat, pwd, etc? So, /usr/bin is where user installed apps are? what are some examples of what should be in /usr/bin? – Patoshi パトシ Oct 5 '14 at 16:49
  • 1
    Does placing scripts in /bin cause any problems? I have moved my scripts according to your answer but I am still curious – Rumesh Apr 27 '15 at 12:14
  • 5
    @RumeshSudhaharan you should not do that, because it's managed by the package management. If any other package also wants to use that path, it will overwrite your file. Files in packages can never overlap (at least for official repositories), so you are more safe when installing in the local path. Also, it's just for your own convenience and safety. In a local path you can't mess up the system in such a bad way and you can't accidentally replace a system binary. – gertvdijk Apr 27 '15 at 12:17
  • 6
    I like the explanation on the error page : Severity:Serious Certainty:Certain – cutrightjm Nov 25 '15 at 6:15
  • I have put some files in /usr/local/bin and I can execute them, however I cannot edit them, ls -la shows nothing – M. Reza Nasirloo Mar 19 at 13:52

I had a similar question myself a year+ ago: Best directory to place my bash scripts?

System directories for binaries

man hier (hierarchy) lists all the directories. To get the ones just for binaries use:

$ man hier | grep -E 'bin$|sbin$|^.{7}(/bin)|^.{7}(/sbin)' -A2

       /bin   This directory contains executable programs which are needed in single user
              mode and to bring the system up or repair it.

--
       /sbin  Like  /bin,  this  directory  holds commands needed to boot the system, but
              which are usually not executed by normal users.

--
       /usr/X11R6/bin
              Binaries  which  belong  to the X-Window system; often, there is a symbolic
              link from the more traditional /usr/bin/X11 to here.
--
       /usr/bin
              This  is the primary directory for executable programs.  Most programs exe‐
              cuted by normal users which are not needed for booting or for repairing the
--
       /usr/local/bin
              Binaries for programs local to the site.

--
       /usr/local/sbin
              Locally installed programs for system administration.

--
       /usr/sbin
              This directory contains program binaries for  system  administration  which
              are  not  essential  for the boot process, for mounting /usr, or for system

Where to put your own scripts?

For all users to access your scripts you can put them in /usr/local/bin. Keep in mind you need sudo access to add / change files here. See: Is there a standard place for placing custom Linux scripts?

For your own user ID scripts put them in /home/YOUR_NAME/bin. Keep in mind you have to create this directory first and relaunch the terminal to get the path automatically setup by ~/.profile. See: How to add /home/username/bin to $PATH?


What I know I don't know

I'm contemplating taking some of my more complex bash scripts in Ask Ubuntu and setting them up with install scripts on github. Here are few examples:

I think the scripts should be installed in /usr/bin which is in the $PATH, but I'm not sure on the appropriate place yet.

  • 1
    I appreciate your effort, but , except the "What I know I don't know" part, you repeated gertvdijk's answer. – danzel Jun 11 at 23:54
  • 1
    @danzel I think the key part is the man hier section which gertvdijk's answer didn't address. ie you can get the directory hierarchical structure in the command line without having to resort to reading it on the web (ie here). – WinEunuuchs2Unix Jun 11 at 23:56
  • 1
    that is interesting information, I agree. But gertvdijk already mentioned the Filesystem Hierarchy Standard (which man hier is an informationally insufficient excerpt of IMO). However, this may be my personal opinion. – danzel Jun 12 at 0:11
  • @danzel I agree websites have a lot more in-depth analysis than what's on our hard drives. I'm only trying to point out the information is on our hard drives without resorting the Internet. Besides the wikipedia link quoted is missing /usr/X11R6/bin and /usr/local/bin which is included in man heir. – WinEunuuchs2Unix Jun 12 at 0:26

protected by WinEunuuchs2Unix Jun 10 at 21:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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