477

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:

2 Answers2

547

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/.

gertvdijk
  • 67,947
  • 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 05 '14 at 16:49
  • 2
    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
  • 10
    @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
  • 14
    I like the explanation on the error page : Severity:Serious Certainty:Certain – cutrightjm Nov 25 '15 at 06:15
  • 2
    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 '18 at 13:52
  • 4
    FWIW, sbin has nothing to do with scripts. It stores binaries - even the links provided note that. –  Sep 14 '18 at 09:58
  • 1
    @M.RezaNasirloo You should try sudo with ls. Might be a permissions issue. – Carolus Dec 26 '19 at 19:00
  • 1
    In order to put user-scoped scripts in ~/bin, you have to include the path ~/bin in the $PATH variable. See https://askubuntu.com/a/195658/1044515 – NAND Jun 26 '21 at 11:50
  • 1
    As @MatthiasBraun pointed out in a comment to the original question, user-scoped scripts should probably go in ~/.local/bin. And it should be added to the $PATH as @NAND points out. – Bob Stein Nov 09 '22 at 20:42
44

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 '18 at 23:54
  • 2
    @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 '18 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 '18 at 00:11
  • 1
    @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 '18 at 00:26
  • @WinEunuuchs2Unix, re last para; Why not /usr/local/bin since you're not the package manager? – Pacerier Jul 20 '19 at 07:09
  • Strangely apt-get and add-apt-repository are in /usr/bin/ instead of /usr/sbin even if they are mostly for system administration. Well, apt-get can be used for displaying changelogs, too, which does not require superuser. – jarno Nov 21 '19 at 11:21