/bin、/sbin、/usr/bin、/usr/sbin、/usr/local/bin、/usr/local/sbin之间的差异

我有六个带有命令文件的目录。它们是/bin/sbin/usr/bin/usr/sbin/usr/local/bin/usr/local/sbin

这些之间有什么区别?如果我自己写脚本,我应该把它们加在哪里?


相关的。

解决办法

这一点请参考Filesystem Hierarchy Standard (FHS) for Linux

  • /bin:用于在挂载/usr分区之前可用的二进制文件。这是在早期启动阶段使用的琐碎的二进制文件,或者在启动单用户模式时需要使用的二进制文件。可以考虑像cat',ls'这样的二进制文件。

  • /sbin:同样的,但用于需要超级用户(root)权限的二进制文件*。

  • /usr/bin:同前,但用于一般系统范围内的二进制文件

  • /usr/sbin:同上,但用于需要超级用户(root)权限的二进制文件。


如果我自己编写脚本,我应该在哪里添加这些内容?

以上都不是。你应该使用/usr/local/bin/usr/local/sbin来编写全系统可用的脚本。local路径意味着它不被系统包管理(这对Debian/Ubuntu包来说是一个错误)。

对于用户范围的脚本,使用~/bin(在你的主目录中的个人bin文件夹)。

FHS说对于/usr/local

本地数据的三级层次结构,特定于这个主机。通常有更多的子目录,例如:bin/lib/share/

评论(6)

一年多前我自己也有过类似的问题: https://askubuntu.com/questions/830074/best-directory-to-place-my-bash-scripts

系统中的二进制文件目录

man hier(层次结构)列出了所有的目录。要获得只用于二进制文件的目录,请使用。

$ 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

在哪里放置你自己的脚本?

为了让所有用户都能访问你的脚本,你可以把它们放在/usr/local/bin中。请记住,你需要sudo权限来添加/更改这里的文件。参见:https://askubuntu.com/questions/195652/is-there-a-standard-place-for-placing-custom-linux-scripts

对于你自己的用户ID脚本,把它们放在/home/YOUR_NAME/bin。请记住,你必须先创建这个目录,然后重新启动终端,以获得~/.profile自动设置的路径。参见:https://askubuntu.com/questions/402353/how-to-add-home-username-bin-to-path?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa


我所知道的我不知道的

我正在考虑把我在问Ubuntu中的一些比较复杂的bash脚本,在github上设置成安装脚本。这里有几个例子。

认为这些脚本应该安装在$PATH中的/usr/bin中,但我还不确定合适的位置。

评论(5)