In-depth understanding of the directory structure linux system - Detailed

Transfer from https://www.jb51.net/LINUXjishu/151820.html

For every Linux learners to understand the directory structure of the Linux file system, Linux is a crucial step to learn., Functional standards and detailed insight into each directory linux file directory structure for us to make good use of linux system just important, here we begin to understand what knowledge linux directory structure.

When in the use of Linux, if you are / will find by ls -l, bear in / many directories, such as etc, usr, var, bin ... ... etc directory, but in these directories, we look inside and found there are a lot of directories or files. File system looks like a tree in Linux, so we can put the image of the file system structure is called a tree structure .

File system is used to organize and access files arrangement, so she is visible, in Linux, we can see its structure through tools such as ls, in a Linux system, we see is the tree; For example, the operating system is installed on a file system performance by his / initial tree structure. The top of the linux file system is / we say / Linux is the root, which is the Linux operating system's file system. Entrance Linux file system is the /, all directories, files, devices are part of the /, / Linux file system is the organizer, but also the most superior leader.

Since linux is open source, companies and organizations to make their operations, according to the linux kernel programming code. This resulted in different directories under the root. This has caused PC can not use other people's personal linux system. Because you do not know where some of the basic configuration files. . . This has resulted in confusion. This is the FHS (Filesystem Hierarchy Standard) reason for the birth of the organization. The agency is a voluntary organization composed of linux enthusiasts, mainly yes yes right linux do some basic requirements, and will not replace is the operator of the host became linux 'illiterate'.

According to FHS (http://www.pathname.com/fhs/) official documents pointed out that their main purpose is to allow users to understand the installed software is usually placed in that directory, so they want an independent software development business, operating system makers, as well as the user wants to maintain the system, are able to follow the FHS. In other words, FHS focus is under the specification for each specific directory data should look like you want to place it .  So good at very much, because the Linux operating system will be able to (change directory schema) in the face of existing developers want to develop a unique style.

Indeed, FHS is based on past experience has been continued for the revision, FHS based file system used frequently or not and whether to allow the user to freely modifiers, and the directory defined to form four kinds of interaction, a table for a bit like the bottom of this:

 

Shareable (shareable)

Not sharing (unshareable)

Constant (static)

/ Usr (software stored)

/ Etc (profile)

/ Opt (third-party software in conjunction)

/ Boot (boot and core files)

Can change (variable)

/ Var / mail (mail box user)

/ Var / run (application dependent)

/ Var / spool / news (newsgroups)

/ Var / lock (application dependent)

Fourth type:

1. shareable:

Other systems can share directory mount use, so execution file including the user data such as e-mail, to be able to share the directory on the network used to mount other hosts;

2. Do not share:

The operation of their machines with the above devices file or program-related socket files, and since only about their own machine, so of course is not suitable for other hosts to share a.

3. unchanged:

Some data is not constantly changing, followed by distribution without changes. Such as libraries, documentation file, the system administrator managed hosting services configuration file, and so on;

4. changes:

Constantly changing data, such as the log file, the general user can accept the newsgroups.

Indeed, FHS directory tree for a three-tier architecture only defines what data should be placed under the catalog only, which are defined under three directories:

/ (Root, root): related to boot the system;

/ Usr (unix software resource): software installation and / enforcing;

/ Var (variable): related to the operation of the process system.

A root directory (/) the meaning and content:

The root directory is the most important one of the whole system , not only because all directories are derived from the root directory, but also the root of the boot / restore / repair systems and other related actions. Because the software requires specific boot system boot, the core file, start the necessary procedures, libraries, etc. file data, if a system error occurs, the root directory must also include a program to repair the file system of the job. Because the root directory is so important, so the requirements of the FHS, he hopes not on the root partition very large, because the larger the partition you will put more and more data, this way the root partition resides on there would be more chance for error occur.

Therefore, the FHS recommendations: root directory (/) where the partition should be as small as possible, and the software applications installed preferably not on the same partition with the root directory, keeping the root of the smaller the better. So not only better performance, the root directory where the file system is also less prone to problems. To put it plainly, it is the root directory of the C drive and a Windows-like.

According to the above reason, FHS that the root directory (/) should contain the following subdirectory:

table of Contents

File content should be placed

/bin

There are many placement system binaries directory, but the / bin special. Since / bin is placed under the single instruction maintenance mode it can also be operated. In / bin under the instruction can be used with the root account the general, mainly: cat, chmod (permission to modify), chown, date, mv, mkdir, cp, bash like commonly used commands.

/boot

The main use of the opportunity to place the file open, and including the Linux kernel files and boot menu and boot the desired profile, and so on. Linux kernel commonly used file named: vmlinuz, if you are using the grub boot manager, it will continue to exist / boot / grub / directory.

/dev

On Linux system, any peripheral device is in the file directory should exist in this type . Simply by accessing a file in this directory is equivalent to access a device. Than to have important files / dev / null, / dev / zero, / dev / tty, / dev / lp *, / dev / hd *, / dev / sd * , etc.

/etc

The main profile system almost all placed in this directory , for example, account password file officers, Kai various services starting stalls, and so on. In general, each attribute file in this directory is that it gives access to the general user, but only root has the power to modify . FHS is not recommended to place an executable file in this directory (binary). There are more important files: / etc / inittab, /etc/init.d/, /etc/modprobe.conf, / etc / X11 /, / etc / fstab, / etc / sysconfig / etc. In addition, under the directory has important : /etc/init.d/: the default for all services start script (startup scripts ) are placed here, for example, to enable or disable iptables words: /etc/init.d / iptables start, / etc / init.d / iptables stop

/etc/xinetd.d/: This is the so-called super daemon service management profile catalog.

/ Etc / X11 /: X Window associated with various profiles are all here, especially xorg.conf or XF86Config both X Server profile.

/home

This is the system default user home directory (home directory). When you add a regular user account, the user's home directory will be the default specifications to be here. More important it is that there are two home directory code: ~: represents the current user's home directory, ~ guest: user name represents the home directory of the guest.

/lib

Very many library systems, and / lib is placed in the boot of the library will be used, as well as in the library (call) / bin or / sbin under the directive would call it.  What is the library it? U can think of is that he will plug, plug some of these instructions must be able to complete the implementation of the program is intended to smooth. Of particular importance is / lib / modules / directory because the directory is placed in the core of related modules (drivers).

/media

media are media in English, as the name implies, this is placed under the media / device may be removable. Including floppy disk, CD, DVD, etc. are temporarily mount this device . Common file name there: / media / floppy, / media / cdrom and so on.

/ mnt

If u want some additional means for temporarily mount, general recommendations u can be placed into this directory. In the old days, when the purpose of this directory / media the same friends. With only / after the media, this directory is used to temporarily mount use.

/opt

This is the third-party software to a third party (third-party software applications) placed in the directory. What is third-party software in conjunction ah? For example, the KDE desktop management system is a stand-alone program, but he can be mounted to a Linux system, so KDE software on the proposal to place under this directory. In addition, if u want to install additional software on their own (non-original distribution provided), it is also possible to install your software here. However, the previous Linux system, we used to be placed in the / usr / local directory.

/root

The system administrator (root) home directory . The reason on here, because if they go into maintenance mode and only single person to mount the root directory, which will be able to have a home directory root, so we want to root's home directory to the root directory placed in the same partition.

/sbin

Linux有非常多指令是用来设定系统环境的,这些指令只有root才能够利用来设定系统,其他使用者最多只能用来查询而已。放在/sbin底下的为开机过程中所需要的,里面包括了开机、修复、还原系统所需要的指令。至于某些伺服器软体程式,一般则放置到/usr/sbin/当中。至于本机自行安装的软体所产生的系统执行档(system binary),则放置到/usr/local/sbin/当中了。常见的指令包括:fdisk, fsck, ifconfig, init, mkfs等等。

/srv

srv可以视为service的缩写,是一些网路服务启动之后,这些服务所需要取用的资料目录。 常见的服务例如WWW, FTP等等。 举例来说,WWW伺服器需要的网页资料就可以放置在/srv/www/里面。呵呵,看来平时我们编写的代码应该放到这里了。

/tmp

这是让一般使用者或者是正在执行的程序暂时放置档案的地方。这个目录是任何人都能够存取的,所以你需要定期的清理一下。当然,重要资料不可放置在此目录啊。 因为FHS甚至建议在开机时,应该要将/tmp下的资料都删除。

事实上FHS针对根目录所定义的标准就仅限于上表,不过仍旧有些目录也需要我们了解一下,具体如下:

 

目录

应放置文件内容

/lost+found

这个目录是使用标准的ext2/ext3档案系统格式才会产生的一个目录,目的在于当档案系统发生错误时,将一些遗失的片段放置到这个目录下。 这个目录通常会在分割槽的最顶层存在,例如你加装一个硬盘于/disk中,那在这个系统下就会自动产生一个这样的目录/disk/lost+found

/proc

这个目录本身是一个虚拟文件系统(virtual filesystem)喔。 他放置的资料都是在内存当中,例如系统核心、行程资讯(process)(是进程吗?)、周边装置的状态及网络状态等等。因为这个目录下的资料都是在记忆体(内存)当中,所以本身不占任何硬盘空间。比较重要的档案(目录)例如: /proc/cpuinfo, /proc/dma, /proc/interrupts, /proc/ioports, /proc/net/*等等。呵呵,是虚拟内存吗[guest]?

/sys

这个目录其实跟/proc非常类似,也是一个虚拟的档案系统,主要也是记录与核心相关的资讯。 包括目前已载入的核心模组与核心侦测到的硬体装置资讯等等。 这个目录同样不占硬盘容量。

除了这些目录的内容之外,另外要注意的是,因为根目录与开机有关,开机过程中仅有根目录会被挂载, 其他分区则是在开机完成之后才会持续的进行挂载的行为。就是因为如此,因此根目录下与开机过程有关的目录, 就不能够与根目录放到不同的分区去。那哪些目录不可与根目录分开呢?有底下这些:

/etc:配置文件

/bin:重要执行档

/dev:所需要的装置文件

/lib:执行档所需的函式库与核心所需的模块

/sbin:重要的系统执行文件

这五个目录千万不可与根目录分开在不同的分区。请背下来啊。 

二. /usr 的意义与内容:

依据FHS的基本定义,/usr里面放置的数据属于可分享的与不可变动的(shareable, static), 如果你知道如何透过网络进行分区的挂载(例如在服务器篇会谈到的NFS服务器),那么/usr确实可以分享给局域网络内的其他主机来使用喔。

/usr不是user的缩写,其实usr是Unix Software Resource的缩写 也就是Unix操作系统软件资源所放置的目录,而不是用户的数据。这点要注意。 FHS建议所有软件开发者,应该将他们的数据合理的分别放置到这个目录下的次目录,而不要自行建立该软件自己独立的目录。

因为是所有系统默认的软件(distribution发布者提供的软件)都会放置到/usr底下,因此这个目录有点类似Windows 系统的C:\Windows\ + C:\Program files\这两个目录的综合体,系统刚安装完毕时,这个目录会占用最多的硬盘容量。 一般来说,/usr的次目录建议有底下这些:

目录

应放置文件内容

/usr/X11R6/ 

为X Window System重要数据所放置的目录,之所以取名为X11R6是因为最后的X版本为第11版,且该版的第6次释出之意。 

/usr/bin/ 

绝大部分的用户可使用指令都放在这里。请注意到他与/bin的不同之处。(是否与开机过程有关) 

/usr/include/ 

c/c++等程序语言的档头(header)与包含档(include)放置处,当我们以tarball方式 (*.tar.gz 的方式安装软件)安装某些数据时,会使用到里头的许多包含档。 

/usr/lib/ 

包含各应用软件的函式库、目标文件(object file),以及不被一般使用者惯用的执行档或脚本(script)。 某些软件会提供一些特殊的指令来进行服务器的设定,这些指令也不会经常被系统管理员操作, 那就会被摆放到这个目录下啦。要注意的是,如果你使用的是X86_64的Linux系统, 那可能会有/usr/lib64/目录产生 

/usr/local/ 

统管理员在本机自行安装自己下载的软件(非distribution默认提供者),建议安装到此目录, 这样会比较便于管理。举例来说,你的distribution提供的软件较旧,你想安装较新的软件但又不想移除旧版, 此时你可以将新版软件安装于/usr/local/目录下,可与原先的旧版软件有分别啦。 你可以自行到/usr/local去看看,该目录下也是具有bin, etc, include, lib...的次目录 

/usr/sbin/ 

非系统正常运作所需要的系统指令。最常见的就是某些网络服务器软件的服务指令(daemon) 

/usr/share/ 

放置共享文件的地方,在这个目录下放置的数据几乎是不分硬件架构均可读取的数据, 因为几乎都是文本文件嘛。在此目录下常见的还有这些次目录:/usr/share/man:联机帮助文件

/usr/share/doc:软件杂项的文件说明

/usr/share/zoneinfo:与时区有关的时区文件

/usr/src/ 

一般原始码建议放置到这里,src有source的意思。至于核心原始码则建议放置到/usr/src/linux/目录下。 

 

三.  /var 的意义与内容:

如果/usr是安装时会占用较大硬盘容量的目录,那么/var就是在系统运作后才会渐渐占用硬盘容量的目录。 因为/var目录主要针对常态性变动的文件,包括缓存(cache)、登录档(log file)以及某些软件运作所产生的文件, 包括程序文件(lock file, run file),或者例如MySQL数据库的文件等等。常见的次目录有:

目录

应放置文件内容

/var/cache/

应用程序本身运作过程中会产生的一些暂存档

/var/lib/

程序本身执行的过程中,需要使用到的数据文件放置的目录。在此目录下各自的软件应该要有各自的目录。 举例来说,MySQL的数据库放置到/var/lib/mysql/而rpm的数据库则放到/var/lib/rpm去

/var/lock/

某些装置或者是文件资源一次只能被一个应用程序所使用,如果同时有两个程序使用该装置时, 就可能产生一些错误的状况,因此就得要将该装置上锁(lock),以确保该装置只会给单一软件所使用。 举例来说,刻录机正在刻录一块光盘,你想一下,会不会有两个人同时在使用一个刻录机烧片? 如果两个人同时刻录,那片子写入的是谁的数据?所以当第一个人在刻录时该刻录机就会被上锁, 第二个人就得要该装置被解除锁定(就是前一个人用完了)才能够继续使用

/var/log/

非常重要。这是登录文件放置的目录。里面比较重要的文件如/var/log/messages, /var/log/wtmp(记录登入者的信息)等。

/var/mail/

放置个人电子邮件信箱的目录,不过这个目录也被放置到/var/spool/mail/目录中,通常这两个目录是互为链接文件。

/var/run/

某些程序或者是服务启动后,会将他们的PID放置在这个目录下

/var/spool/

这个目录通常放置一些队列数据,所谓的“队列”就是排队等待其他程序使用的数据。 这些数据被使用后通常都会被删除。举例来说,系统收到新信会放置到/var/spool/mail/中, 但使用者收下该信件后该封信原则上就会被删除。信件如果暂时寄不出去会被放到/var/spool/mqueue/中, 等到被送出后就被删除。如果是工作排程数据(crontab),就会被放置到/var/spool/cron/目录中。

 由于FHS仅是定义出最上层(/)及次层(/usr, /var)的目录内容应该要放置的文件或目录数据, 因此,在其他次目录层级内,就可以随开发者自行来配置了。

四. 目录树(directory tree) :

在Linux底下,所有的文件与目录都是由根目录开始的。那是所有目录与文件的源头, 然后再一个一个的分支下来,因此,我们也称这种目录配置方式为:目录树(directory tree), 这个目录树的主要特性有:

目录树的启始点为根目录 (/, root);

每一个目录不止能使用本地端的 partition 的文件系统,也可以使用网络上的 filesystem 。举例来说, 可以利用 Network File System (NFS) 服务器挂载某特定目录等。

每一个文件在此目录树中的文件名(包含完整路径)都是独一无二的。

如果我们将整个目录树以图的方法来显示,并且将较为重要的文件数据列出来的话,那么目录树架构就如下图所示:

linux目录树

五. 绝对路径与相对路径

除了需要特别注意的FHS目录配置外,在文件名部分我们也要特别注意。因为根据档名写法的不同,也可将所谓的路径(path)定义为绝对路径(absolute)与相对路径(relative)。 这两种文件名/路径的写法依据是这样的:

绝对路径:

由根目录(/)开始写起的文件名或目录名称, 例如 /home/dmtsai/.bashrc;

相对路径:

相对于目前路径的文件名写法。 例如 ./home/dmtsai 或 http://www.cnblogs.com/home/dmtsai/ 等等。反正开头不是 / 就属于相对路径的写法

而你必须要了解,相对路径是以你当前所在路径的相对位置来表示的。举例来说,你目前在 /home 这个目录下, 如果想要进入 /var/log 这个目录时,可以怎么写呢?

 命令如下:

cd /var/log (absolute) cd ../var/log (relative)

因为你在 /home 底下,所以要回到上一层 (../) 之后,才能继续往 /var 来移动的,特别注意这两个特殊的目录:

.  :代表当前的目录,也可以使用 ./ 来表示;

.. :代表上一层目录,也可以 ../ 来代表。

这个 . 与 .. 目录概念是很重要的,你常常会看到 cd .. 或 ./command 之类的指令下达方式, 就是代表上一层与目前所在目录的工作状态。

实例1:如何先进入/var/spool/mail/目录,再进入到/var/spool/cron/目录内?

命令如下:

cd /var/spool/mail
cd ../cron

说明:

由于/var/spool/mail与/var/spool/cron是同样在/var/spool/目录中。如此就不需要在由根目录开始写起了。这个相对路径是非常有帮助的,尤其对于某些软件开发商来说。 一般来说,软件开发商会将数据放置到/usr/local/里面的各相对目录。 但如果用户想要安装到不同目录呢?就得要使用相对路径。

实例2:网络文件常常提到类似./run.sh之类的数据,这个指令的意义为何?

说明:

由于指令的执行需要变量的支持,若你的执行文件放置在本目录,并且本目录并非正规的执行文件目录(/bin, /usr/bin等为正规),此时要执行指令就得要严格指定该执行档。./代表本目录的意思,所以./run.sh代表执行本目录下, 名为run.sh的文件。

Guess you like

Origin www.cnblogs.com/shineriver/p/11563852.html