Skip to content

Linux File System Overview

Linux File System Overview

Filesystem Hierarchy Standard (FHS), which is part of the LSB (Linux Standards Base) specifications.

The Root directory "/". Refers to the highest layer of the file system tree. This root partition is mounted first at system boot. All programs that are run at system startup must be in this partition.

The following directories must be in the root partition:

  • /bin - User binaries. 基本程序
    • Contains executables required when no other file systems are mounted. For example, programs required for system booting, working with files and configuration.
    • /bin/bash - The bash shell
    • /bin/cat - Display file contents
    • /bin/cp - Copy files
    • /bin/dd - Copy files byte-wise
    • /bin/gzip - Compress files
    • /bin/mount - Mount file systems
    • /bin/rm - Delete files
    • /bin/vi - Edit files
  • /sbin - System binaries. 系统程序
    • Contains programs important for system administration. 存放系统管理的程序
    • Typically are intended to be run by the root user and therefore it is not in the regular users path. 默认是root用户有权限执行
    • Some important files:
      • /sbin/yast - Administration tool
      • /sbin/fdisk* - Modifies partitions
      • /sbin/fsck* - File system check 不能在运行的系统上面直接执行fsck,损坏根文件系统,需要umount
      • /sbin/mkfs - Creates file systems
      • /sbin/shutdown - Shuts down the system
  • /dev - Device files
    • Each system hardware component is represented (except network cards, which are kernel modules). 以太网卡是内核模块,其他硬件都以设备dev的方式展现
    • Applications read from and write to these files to address hardware components.
    • Two kinds of device files:
      • Character-oriented – Sequential devices (printer, tape and mouse) 字符设备
      • Block-oriented – Hard disks and DVDs 块设备
    • Connections to device drivers are implemented in the kernel using channels called major device numbers. 与设备驱动程序的连接通过内核中称为主设备号的通道实现。
    • When using ls -l the file size is replaced with the device numbers, such as 8, 0.
    • In the past these files were created manually using the mknod command. Today they are created automatically (by udev) when the devices are discovered by the kernel.
    • Some important device files:
      • Null device: - /dev/null
      • Zero device: - /dev/zero
      • System Console: - /dev/console
      • Virtual Terminal: - /dev/tty1
      • Serial ports - /dev/ttyS0
      • Parallel port: - /dev/lp0
      • Floppy disk drive: - /dev/fd0
      • Hard drive: - /dev/sda
      • Hard disk partition: - /dev/sda1
      • CD-ROM drive: - /dev/scd0
  • /etc - Configuration files
    • Contains system and services configuration files. 存放系统和服务的配置文件
    • Most of these files are ASCII files. 大部分都是ASCII文件
    • Normal users can read most of these by default. This can be a security issue since some of these files contain passwords so it important that these files are only readable by the rootuser. 普通用户可以默认读取其中的大部分内容。 这可能是一个安全问题,因为其中一些文件包含密码,因此重要的是这些文件只能由root用户读取
    • No executables can be put here according to the FHS, however subdirectories may contain shell scripts. 根据FHS,此处不能放置任何可执行文件,但子目录可能包含shell脚本。
    • Almost every installed service has at least one configuration file in /etc or a subdirectory. 几乎每个已安装的服务在/ etc或子目录中至少有一个配置文件。
    • Some important configuration files:
      • /etc/SuSE-release - Version of installed system
      • /etc/DIR_COLORS - Colors for the ls command
      • /etc/fstab - For file systems to be mounted
      • /etc/profile - Shell login script
      • /etc/passwd - User database, except passwords
      • /etc/shadow - Password and password info
      • /etc/group - Database of user groups
      • /etc/cups/* - For the CUPS printing system (CUPS=Common UNIX Printing System)
      • /etc/hosts - Host names to IP addresses
      • /etc/motd - Message after login
      • /etc/issue - Message before login
      • /etc/sysconfig/* - System configuration files
  • /lib - Libraries.
    • Many programs have common functions they need. The functions can be kept in a shared library.
    • Libraries are called shared objects and end with the .so extension. 共享库
    • Libraries in /lib are used by programs in /bin and /sbin.
    • There are additional libraries in subdirectories.
    • Kernel modules are located in /lib/modules.
  • /lib64 - 64-Bit Libraries. Similar to the /lib directory.
    • This is an architecture dependent directory.
    • Some systems support different binary formats and keep different versions of the same shared library.
  • /usr - Contains application programs, graphical interface files, libraries, local programs, documentation and more.
    • /usr means Unix System Resources. Examples:
    • /usr/X11R6/ - X Window System Files
    • /usr/bin/ - Almost all executables
    • /usr/lib/ - Libraries and application directories
    • /usr/local/ - Locally installed programs (i.e. on local system if /usr is mounted from the network). The content is not overwritten by system updates. 下面3个目录在初始安装后是空的
      • /usr/local/bin-
      • /usr/local/sbin-
      • /usr/local/lib-
    • /usr/sbin/ - System administration programs
    • /usr/share/doc/ - Documentation
    • /usr/src/ - Source code of kernel and programs
      • /usr/src/linux-
    • /usr/share/man/ - Manual pages
  • /opt - Optional Application Directory
    • Where optional or third party applications that are not considered to be “part of the distribution” store their static files.
    • Applications considered to be “part of the distribution” are usually installed under /usr/lib/ rather than /opt.
    • At installation a directory is created for each application's files with the name of the application. Example:
      • /opt/novell-
  • /boot - The Boot Directory
    • /boot/grub2 - Contains static boot loader files for GRUB2. (GRUB = Grand Unified Boot Loader)
    • Contains the kernel and initrd file identified with the links vmlinuz and initrd.
  • /root - Administrator's Home Directory
    • The root user's home directory. Not under /home with regular users' home directories.
    • Needs to be in the root partition so that root can always log in with his configured environment.
  • /home - User Directories
    • Every system user has an assigned file area which becomes the current working directory after log in. By default they exist in /home.
    • The files and directories in /home could be in a separate partition or on another computer on the network.
    • The user profile and configuration files are found here:
      • .profile - Private user login script
      • .bashrc - Configuration file for bash
      • .bash_history - Previous commands run in bash
  • /run/media/<user>/* - Mount Point for Removable Media
    • SLE 12 creates directories here for mounting removable media. The name depends on the device that is mounted/discovered. Examples:
      • /run/media/media_name/ (Created if labeled media is inserted)
      • /run/media/cdrom/-
      • /run/media/dvd/-
      • /run/media/usbdisk/-
  • /mnt - Temporarily Mounted File Systems 文件系统临时挂载点
    • Standard directory for integrating file systems that are used temporarily.
    • File systems are mounted using the mount command and removed using the umount command.
    • Subdirectories do not exist by default and are not automatically created.
  • /srv - Service Data Directories
    • Contains subdirectories for various services. Examples: 存放各种服务的数据
      • /srv/www - for the Apache Web Server
      • /srv/ftp - for an FTP server
  • /var - Variable Files
    • Contains files that can be modified while the system is running. 在系统运行过程中会被修改的文件
    • Important subdirectories:
      • /var/lib/ - Variable libraries, like databases 可变库文件
      • /var/log/ - Services log files 日志文件
      • /var/run/ - Information on running processes 运行中的线程的信息
      • /var/spool/ - Queues (printers, email)
        • /var/spool/mail-
        • /var/spool/cron-
      • /var/lock/ - Lock files for multiuser access
      • /var/cache-
      • /var/mail-
  • /tmp - Temporary Area
    • Where programs create temporary files while they are running
  • /proc - Process Files
    • A virtual file system that exists only in memory and is used to display the current state of processes running on the system. (Takes no space - file size always 0) 虚拟文件系统,不占间,大小* 始终零,显示当前进程的状态信息
    • Directories containing information about individual processes are named according to the PID number of the process.
    • Some values can be modified to change how things are running in real time. Any changes made are lost at reboot. Examples: 有些值可以临时在线更改生效,但重启后丢失
      • /proc/cpuinfo/ - Processor information
      • /proc/dma/ - Use of DMA ports
      • /proc/interrupts/ - Use of interrupts
      • /proc/ioports/ - Use of I/O ports
      • /proc/filesystems/ - File system formats the kernel knows
      • /proc/modules/ - Active modules
      • /proc/mounts/ - Mounted file systems
      • /proc/net/* - Network information and statistics
      • /proc/partitions/ - Existing partitions
      • /proc/bus/pci/ - Connected PCI devices
      • /proc/bus/scsi/ - Connected SCSI devices
      • /proc/sys/* - System and kernel information
      • /proc/version - Kernel version
  • /sys - System Information Directory A virtual file system that exists only in memory. Takes no space so file size always 0 虚拟文件系统 Provides information on:
    • hardware buses
    • hardware devices
    • active devices
    • drivers

Lab: Explore Filesystem Hierarchy

Show the directory structure of the /data folder hierarchy of current logon user:

mySUSE:~ # tree /data
/data
└── linktype
    ├── file
    ├── hardlinkfile1
    ├── hardlinkfile2
    ├── symlinkfile1 -> file
    ├── symlinkfile1-1 -> symlinkfile1
    └── symlinkfile2 -> file

Show only directories in the /data hierarchhy, not the files in them:

mySUSE:~ # tree -d /data
/data
└── linktype

Show the files and directories in the /data hierarchy, including the full path and filename of each object.

mySUSE:~ # tree -f /data
/data
└── /data/linktype
    ├── /data/linktype/file
    ├── /data/linktype/hardlinkfile1
    ├── /data/linktype/hardlinkfile2
    ├── /data/linktype/symlinkfile1 -> file
    ├── /data/linktype/symlinkfile1-1 -> symlinkfile1
└── /data/linktype/symlinkfile2 -> file

Seven Different types of files

Normal files, examples:

  • ASCII text files
  • Executable files
  • Graphics files

Directories

  • Organize files on the disk
  • Contain files and subdirectories
  • Implement the hierarchical file system

Links

  • Hard links
    • Secondary file names for files on the disk
    • Multiple file names referencing a single inode
    • Referenced file must reside in the same file system
  • Symbolic links:
    • References to other files on the disk
    • The inode contains a reference to another file name
    • .Referenced files can exist in the same file system or in other file systems
    • A symbolic link can reference a non-existent file (broken link)

Sockets - Used for two-way communication between processes. 套接字

Pipes (FIFOs) - Used for one-way communication from one process to another. 管道

Block Devices 块设备

Character Devices 字符设备

Hard links: A hard link is a directory reference, or pointer, to a file on a storage volume. The name associated with the file is a label stored in a directory structure that refers the operating system to the file data. As such, more than one name can be associated with the same file. When accessed through different names, any changes made will affect the same file data. 硬链接是存储卷上文件的目录引用或指针。 文件名是存储在目录结构中的标签,目录结构指向文件数据。 因此,可以将多个文件名与同一文件关联。 通过不同的文件名访问时,所做的任何更改都是针对源文件数据。

Symbolic links: A symbolic link contains a text string that is interpreted and followed by the operating system as a path to another file or directory. It is a file on its own and can exist independently of its target. If a symbolic link is deleted, its target remains unaffected. If the target is moved, renamed or deleted, any symbolic link that used to point to it continues to exist but now points to a non-existing file. 符号链接包含一个文本字符串,操作系统将其解释并作为另一个文件或目录的路径。 它本身就是一个文件,可以独立于目标而存在。 如果删除了符号链接,则其目标不受影响。 如果移动,重命名或删除目标,则用于指向它的任何符号链接将继续存在,但现在指向不存在的文件。

Hard links can only be used when both the file and the link are in the same file system (on the same partition), because inode numbers are only unique within the same file system. You create a hard link by using the ln command, which points to the inode of an already existing file. Thereafter, the file can be accessed under both names–that of the file and that of the link, and you can no longer discern which name existed first or how the original file and the link differ. 仅当文件和链接文件位于同一文件系统(在同一分区上)时,才能使用硬链接,因为inode编号在同一文件系统中仅是唯一的。 您可以使用ln命令创建硬链接,该命令指向已存在文件的inode。 此后,可以在文件的名称和链接的名称下访问文件,并且无法再识别首先存在的名称或原始文件和链接的不同之处。

You can create a symbolic link with the ln command and the -s option. A symbolic link is assigned its own inode—the link refers to a file, so a distinction can always be made between the link and the actual file. 软连接可以针对目录,硬连接只能针对文件。

A file system is essentially a database that is used to keep track of files in a volume. For normal files, data blocks are allocated to store the file's data, an inode is allocated to point to the data blocks as well as store the metadata about the file and then a file name is assigned to the inode. A hard link is a secondary file name associated with an existing inode. For symbolic links, a new inode is allocated with a new file name associated with it but the inode references another file name rather than referencing datablocks. 文件系统本质上是一个用于跟踪卷中文件的数据库。 对于普通文件,分配数据块以存储文件的数据,分配inode以指向数据块以及存储关于文件的元数据,然后将文件名分配给inode。 硬链接是与现有inode关联的辅助文件名。 对于符号链接,将为新的inode分配一个与之关联的新文件名,但inode引用另一个文件名而不是引用数据块。

A good way to see the relationship between file names and inodes is to use the ls -il command. The typical size of an inode is 128 Bit and data blocks can range in size from 1k, 2k, 4k or larger depending on the file system type. 查看文件名和inode之间关系的好方法是使用ls -il命令。inode的典型大小为128位,数据块的大小范围可以是1k,2k,4k或更大,具体取决于文件系统类型。

硬链接相当于增加了一个登记项,使得原来的文件多了一个名字,至于inode都没变。所谓的登记项其实是目录文件中的一个条目(目录项),使用hard link 是让多个不同的目录项指向同一个文件的inode,没有多余的内容需要存储在磁盘扇区中,所以hardlink不占用额外的空间。

符号链接有单独的inode,在inode中存放另一个文件的路径而不是文件数据,所以符号链接会占用额外的空间。

Create original file

mySUSE:/data/linktype # echo "it's original file" > file
mySUSE:/data/linktype # l
-rw-r--r-- 1 root root 19 Mar 28 15:20 file

Create hardlink file (注意file、hardlinkfile1、hardlinkfile2的link位置的数值的变化[红色])

mySUSE:/data/linktype # ln file hardlinkfile1
mySUSE:/data/linktype # ln -s file symlinkfile1
mySUSE:/data/linktype # ln -s file symlinkfile2

mySUSE:/data/linktype # l
-rw-r--r-- 2 root root 19 Mar 28 15:20 file
-rw-r--r-- 2 root root 19 Mar 28 15:20 hardlinkfile1
lrwxrwxrwx 1 root root  4 Mar 28 15:21 symlinkfile1 -> file
lrwxrwxrwx 1 root root  4 Mar 28 15:23 symlinkfile2 -> file

mySUSE:/data/linktype # ln file hardlinkfile2

mySUSE:/data/linktype # l
-rw-r--r-- 3 root root  19 Mar 28 15:20 file (包括自己,一共有3个硬链接)
-rw-r--r-- 3 root root  19 Mar 28 15:20 hardlinkfile1 (继承了原文件的硬链接数量)
-rw-r--r-- 3 root root  19 Mar 28 15:20 hardlinkfile2 (继承了原文件的硬链接数量)
lrwxrwxrwx 1 root root   4 Mar 28 15:21 symlinkfile1 -> file
lrwxrwxrwx 1 root root   4 Mar 28 15:23 symlinkfile2 -> file

Modify content of file (original file). Content change were shown in all hard/soft link files

mySUSE:/data/linktype # echo "add oneline" >> file

mySUSE:/data/linktype # cat file
it's original file
add oneline

mySUSE:/data/linktype # cat hardlinkfile1
it's original file
add oneline

mySUSE:/data/linktype # cat hardlinkfile2
it's original file
add oneline

mySUSE:/data/linktype # cat symlinkfile1
it's original file
add oneline

mySUSE:/data/linktype # cat symlinkfile2
it's original file
add oneline

To view the value stored in a symbolic link use the command readlink.

mySUSE:/data/linktype # ln -s symlinkfile1 symlinkfile1-1

mySUSE:/data/linktype # ls -il
258 -rw-r--r-- 3 root root 31 Mar 28 15:42 file
258 -rw-r--r-- 3 root root 31 Mar 28 15:42 hardlinkfile1
258 -rw-r--r-- 3 root root 31 Mar 28 15:42 hardlinkfile2
259 lrwxrwxrwx 1 root root  4 Mar 28 15:21 symlinkfile1 -> file
265 lrwxrwxrwx 1 root root 12 Mar 28 15:49 symlinkfile1-1 -> symlinkfile1
260 lrwxrwxrwx 1 root root  4 Mar 28 15:23 symlinkfile2 -> file

mySUSE:/data/linktype # readlink symlinkfile1
file

mySUSE:/data/linktype # readlink symlinkfile2
file

mySUSE:/data/linktype # readlink symlinkfile1-1
symlinkfile1  (注意:这仍然是一个符号链接文件)

mySUSE:/data/linktype # readlink -f symlinkfile1-1(参数-f可以直接定位真正的源文件)
/data/linktype/file (注意:这才是真正的原文件)

Linux Device File

Represent hardware (except network cards). Each piece of hardware is represented by a device file. Network cards are interfaces. (区别)

Link between hardware devices and the kernel drivers 设备文件把内核驱动和物理硬件设备连接起来

Kernel drivers read from and write to the device file 内核驱动程序对设备文件进行读写来实现对硬件的读写

The kernel gets the data to the actual hardware in the correct format 内核以正确的格式对物理设备进行读写

Types:

  • Block Devices. A block device reads/writes information in (normally) 512 byte large blocks.
  • Character Devices. A character device reads/writes information character wise. Character devices provide unbuffered access directly to a hardware device. 直接读写,不通过缓存
    • Sometimes referred to as raw devices. 裸设备(注意:裸设备被视为字符设备,不是块设备)
    • any different options for character devices, making their use and application wide and varied.
  • Created automatically by the OS (udev) when the device is discovered by the kernel. 内核直接创建对应硬件的设备文件