下载APP

8.第三章 Linux文件管理和IO重定向 -- 文件系统目录结构(一)

“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 4 天,点击查看活动详情

第三章 Linux文件管理和IO重定向

内容概述

  • 文件系统目录结构
  • 创建和查看文件
  • 复制、转移和删除文件
  • 软和硬链接
  • IO 重定向和管道

1.文件系统目录结构

在这里插入图片描述

1.1 文件系统的目录结构

  • 文件和目录被组织成一个单根倒置树结构
  • 文件系统从根目录下开始,用“/”表示
  • 根文件系统(rootfs):root filesystem
  • 标准Linux文件系统(如:ext4),文件名称大小写敏感,例如:MAIL, Mail, mail, mAiL
  • 以 . 开头的文件为隐藏文件
  • 路径分隔的 /
  • 文件名最长255个字节
  • 包括路径在内文件名称最长4095个字节
  • 蓝色-->目录 绿色-->可执行文件 红色-->压缩文件 浅蓝色-->链接文件 灰色-->其他文件
  • 除了斜杠和NUL,所有字符都有效.但使用特殊字符的目录名和文件不推荐使用,有些字符需要用引号来引用
  • 每个文件都有两类相关数据:元数据:metadata,即属性, 数据:data,即文件内容

Linux的文件系统分层结构:FHS Filesystem Hierarchy Standard

参考文档:http://www.pathname.com/fhs/

1.2 常见的文件系统目录功能

/boot:引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader, grub)都存放于此目录
/bin:所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
/sbin:管理类的基本命令;不能关联至独立分区,OS启动即会用到的程序
/lib:启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules)
/lib64:专用于x86_64系统上的辅助共享库文件存放位置
/etc:配置文件目录
/home/USERNAME:普通用户家目录
/root:管理员的家目录
/media:便携式移动设备挂载点
/mnt:临时文件系统挂载点
/dev:设备文件及特殊文件存储位置
    b: block device,随机访问
    c: character device,线性访问
/opt:第三方应用程序的安装位置
/srv:系统上运行的服务用到的数据
/tmp:临时文件存储位置
/usr: universal shared, read-only data
    bin: 保证系统拥有完整功能而提供的应用程序
    sbin:
    lib:32位使用
    lib64:只存在64位系统
    include: C程序的头文件(header files)
    share:结构化独立的数据,例如doc, man等
        local:第三方应用程序的安装位置
            bin, sbin, lib, lib64, etc, share
/var: variable data files
    cache: 应用程序缓存数据目录
    lib: 应用程序状态信息数据
    local:专用于为/usr/local下的应用程序存储可变数据
    lock: 锁文件
    log: 日志目录及文件
    opt: 专用于为/opt下的应用程序存储可变数据
    run: 运行中的进程相关数据,通常用于存储进程pid文件
    spool: 应用程序数据池
    tmp: 保存系统两次重启之间产生的临时数据
/proc: 用于输出内核与进程信息相关的虚拟文件系统
/sys:用于输出当前系统上硬件设备相关信息虚拟文件系统
/selinux: security enhanced Linux,selinux相关的安全策略等信息的存储位置

1.3 应用程序的组成部分

二进制程序:/bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin
库文件:/lib, /lib64, /usr/lib, /usr/lib64, /usr/local/lib, /usr/local/lib64
配置文件:/etc, /etc/DIRECTORY, /usr/local/etc
帮助文件:/usr/share/man, /usr/share/doc, /usr/local/share/man,
/usr/local/share/doc

1.4 CentOS 7 以后版本目录结构变化

  • /bin 和 /usr/bin
  • /sbin 和 /usr/sbin
  • /lib 和/usr/lib
  • /lib64 和 /usr/lib64

范例:

[root@rocky8 ~]# ls /bin /sbin /lib /lib64 -ld  #centos 7以后/bin /sbin /lib /lib64 都是软链接
lrwxrwxrwx. 1 root root 7 Mar 15  2021 /bin -> usr/bin
lrwxrwxrwx. 1 root root 7 Mar 15  2021 /lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 15  2021 /lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 8 Mar 15  2021 /sbin -> usr/sbin

[root@centos6 ~]# ls /bin /sbin /lib /lib64 -ld #centos 6之前 /bin /sbin /lib /lib64都是单独目录
dr-xr-xr-x. 2 root root  4096 Oct  2 14:41 /bin
dr-xr-xr-x. 9 root root  4096 Oct  2 14:41 /lib
dr-xr-xr-x. 7 root root 12288 Oct  2 14:41 /lib64
dr-xr-xr-x. 2 root root  4096 Oct  2 14:41 /sbin

root@ubuntu1804:~# ls /bin /sbin /lib /lib64 -ld  #ubuntu /bin /sbin /lib /lib64都是单独目录
drwxr-xr-x  2 root root  4096 Sep 29 00:24 /bin
drwxr-xr-x 21 root root  4096 Sep 30 19:17 /lib
drwxr-xr-x  2 root root  4096 Sep 29 00:07 /lib64
drwxr-xr-x  2 root root 12288 Sep 29 00:28 /sbin

1.5 Linux下的文件类型

  • - 普通文件
  • d 目录文件directory
  • b 块设备block
  • c 字符设备character
  • l 符号链接文件link
  • p 管道文件pipe
  • s 套接字文件socket

范例:

root@ubuntu1804:~# ls -l /run/
total 28
srw-rw-rw-  1 root root    0 Oct  2 14:18 acpid.socket # s 套接字文件socket
-rw-------  1 root root    0 Oct  2 14:18 agetty.reload # - 普通文件
-rw-r--r--  1 root root    4 Oct  2 14:18 atd.pid 
drwxr-xr-x  2 root root   60 Oct  2 14:18 blkid # d 目录文件directory
drwxr-xr-x  2 root root   80 Oct  2 14:18 console-setup
-rw-r--r--  1 root root    4 Oct  2 14:18 crond.pid
----------  1 root root    0 Oct  2 14:18 crond.reboot
drwx------  2 root root   40 Oct  2 14:18 cryptsetup
drwxr-xr-x  2 root root   60 Oct  2 14:18 dbus
prw-------  1 root root    0 Oct  2 14:18 dmeventd-client # p 管道文件pipe
prw-------  1 root root    0 Oct  2 14:18 dmeventd-server
drwxr-xr-x  2 root root   60 Oct  2 14:18 fsck
lrwxrwxrwx  1 root root   25 Oct  2 14:18 initctl -> /run/systemd/initctl/fifo # l 符号链接文件link
drwxr-xr-x  2 root root  100 Oct  2 14:18 initramfs
drwxrwxrwt  4 root root   80 Oct  2 14:18 lock
drwxr-xr-x  2 root root   40 Oct  2 14:18 log
drwx------  2 root root   80 Oct  2 14:18 lvm
-rw-r--r--  1 root root    4 Oct  2 14:18 lvmetad.pid
-rw-r--r--  1 root root  624 Oct  2 14:18 motd.dynamic
drwxr-xr-x  2 root root   60 Oct  2 14:18 mount
drwxr-xr-x  2 root root   60 Oct  2 14:18 network
drwxr-xr-x  3 root root   60 Oct  2 14:18 NetworkManager
-rw-r--r--  1 root root    3 Oct  2 14:18 rsyslogd.pid
drwxrwxrwt  2 root utmp   40 Oct  2 14:18 screen
drwxr-xr-x  2 root root   40 Oct  2 14:18 sendsigs.omit.d
lrwxrwxrwx  1 root root    8 Oct  2 14:18 shm -> /dev/shm
srw-rw-rw-  1 root root    0 Oct  2 14:18 snapd-snap.socket
srw-rw-rw-  1 root root    0 Oct  2 14:18 snapd.socket
drwxr-xr-x  2 root root   40 Oct  2 14:18 sshd
-rw-r--r--  1 root root    4 Oct  2 14:18 sshd.pid
drwx--x--x  3 root root   60 Oct  2 14:18 sudo
drwxr-xr-x 22 root root  520 Oct  2 14:18 systemd
drwxr-xr-x  2 root root   60 Oct  2 14:18 tmpfiles.d
drwxr-xr-x  7 root root  200 Oct  2 14:18 udev
drwxr-xr-x  3 root root   60 Oct  2 14:18 user
-rw-rw-r--  1 root utmp 1536 Oct  2 14:18 utmp
drwxr-xr-x  2 root root   60 Oct  2 14:18 uuidd
drwxr-xr-x  2 root root   60 Oct  2 14:18 vmware

[root@rocky8 ~]# ll /dev
total 0
crw-r--r-- 1 root root     10, 235 Oct  2 15:21 autofs # c 字符设备character
drwxr-xr-x 2 root root         180 Oct  2 15:21 block
drwxr-xr-x 2 root root          80 Oct  2 15:21 bsg
drwxr-xr-x 3 root root          60 Oct  2 15:21 bus
lrwxrwxrwx 1 root root           3 Oct  2 15:21 cdrom -> sr0
drwxr-xr-x 2 root root        2860 Oct  2 15:21 char
crw------- 1 root root      5,   1 Oct  2 15:21 console
lrwxrwxrwx 1 root root          11 Oct  2 15:21 core -> /proc/kcore
drwxr-xr-x 3 root root          60 Oct  2 15:21 cpu
crw------- 1 root root     10,  62 Oct  2 15:21 cpu_dma_latency
drwxr-xr-x 7 root root         140 Oct  2 15:21 disk
crw-rw---- 1 root audio    14,   9 Oct  2 15:21 dmmidi
drwxr-xr-x 3 root root         100 Oct  2 15:21 dri
crw-rw---- 1 root video    29,   0 Oct  2 15:21 fb0
lrwxrwxrwx 1 root root          13 Oct  2 15:21 fd -> /proc/self/fd
crw-rw-rw- 1 root root      1,   7 Oct  2 15:21 full
crw-rw-rw- 1 root root     10, 229 Oct  2 15:21 fuse
crw------- 1 root root    245,   0 Oct  2 15:21 hidraw0
crw------- 1 root root     10, 228 Oct  2 15:21 hpet
drwxr-xr-x 2 root root           0 Oct  2 15:21 hugepages
crw------- 1 root root     10, 183 Oct  2 15:21 hwrng
lrwxrwxrwx 1 root root          12 Oct  2 15:21 initctl -> /run/initctl
drwxr-xr-x 4 root root         280 Oct  2 15:21 input
crw-r--r-- 1 root root      1,  11 Oct  2 15:21 kmsg
lrwxrwxrwx 1 root root          28 Oct  2 15:21 log -> /run/systemd/journal/dev-log
crw-rw---- 1 root disk     10, 237 Oct  2 15:21 loop-control
drwxr-xr-x 2 root root          60 Oct  2 15:21 mapper
crw------- 1 root root     10, 227 Oct  2 15:21 mcelog
crw-r----- 1 root kmem      1,   1 Oct  2 15:21 mem
crw-rw---- 1 root audio    14,   2 Oct  2 15:21 midi
drwxrwxrwt 2 root root          40 Oct  2 15:21 mqueue
drwxr-xr-x 2 root root          60 Oct  2 15:21 net
crw-rw-rw- 1 root root      1,   3 Oct  2 15:21 null
crw------- 1 root root     10, 144 Oct  2 15:21 nvram
crw-r----- 1 root kmem      1,   4 Oct  2 15:21 port
crw------- 1 root root    108,   0 Oct  2 15:21 ppp
crw-rw-rw- 1 root tty       5,   2 Oct  2 16:53 ptmx
drwxr-xr-x 2 root root           0 Oct  2 15:21 pts
crw-rw-rw- 1 root root      1,   8 Oct  2 15:21 random
drwxr-xr-x 2 root root          60 Oct  2 15:21 raw
crw-rw-r-- 1 root root     10,  60 Oct  2 15:21 rfkill
lrwxrwxrwx 1 root root           4 Oct  2 15:21 rtc -> rtc0
crw------- 1 root root    251,   0 Oct  2 15:21 rtc0
brw-rw---- 1 root disk      8,   0 Oct  2 15:21 sda # b 块设备block
brw-rw---- 1 root disk      8,   1 Oct  2 15:21 sda1
brw-rw---- 1 root disk      8,   2 Oct  2 15:21 sda2
brw-rw---- 1 root disk      8,   3 Oct  2 15:21 sda3
brw-rw---- 1 root disk      8,   4 Oct  2 15:21 sda4
brw-rw---- 1 root disk      8,   5 Oct  2 15:21 sda5
crw-rw---- 1 root disk     21,   0 Oct  2 15:21 sg0
crw-rw---- 1 root cdrom    21,   1 Oct  2 15:21 sg1
drwxrwxrwt 2 root root          40 Oct  2 15:21 shm
crw------- 1 root root     10, 231 Oct  2 15:21 snapshot
drwxr-xr-x 3 root root         200 Oct  2 15:21 snd
brw-rw---- 1 root cdrom    11,   0 Oct  2 15:21 sr0
lrwxrwxrwx 1 root root          15 Oct  2 15:21 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root          15 Oct  2 15:21 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root          15 Oct  2 15:21 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root tty       5,   0 Oct  2 15:21 tty
crw--w---- 1 root tty       4,   0 Oct  2 15:21 tty0
crw--w---- 1 root tty       4,   1 Oct  2 15:21 tty1
crw--w---- 1 root tty       4,  10 Oct  2 15:21 tty10
crw--w---- 1 root tty       4,  11 Oct  2 15:21 tty11
crw--w---- 1 root tty       4,  12 Oct  2 15:21 tty12
crw--w---- 1 root tty       4,  13 Oct  2 15:21 tty13
crw--w---- 1 root tty       4,  14 Oct  2 15:21 tty14
crw--w---- 1 root tty       4,  15 Oct  2 15:21 tty15
crw--w---- 1 root tty       4,  16 Oct  2 15:21 tty16
crw--w---- 1 root tty       4,  17 Oct  2 15:21 tty17
crw--w---- 1 root tty       4,  18 Oct  2 15:21 tty18
crw--w---- 1 root tty       4,  19 Oct  2 15:21 tty19
crw--w---- 1 root tty       4,   2 Oct  2 15:21 tty2
crw--w---- 1 root tty       4,  20 Oct  2 15:21 tty20
crw--w---- 1 root tty       4,  21 Oct  2 15:21 tty21
crw--w---- 1 root tty       4,  22 Oct  2 15:21 tty22
crw--w---- 1 root tty       4,  23 Oct  2 15:21 tty23
crw--w---- 1 root tty       4,  24 Oct  2 15:21 tty24
crw--w---- 1 root tty       4,  25 Oct  2 15:21 tty25
crw--w---- 1 root tty       4,  26 Oct  2 15:21 tty26
crw--w---- 1 root tty       4,  27 Oct  2 15:21 tty27
crw--w---- 1 root tty       4,  28 Oct  2 15:21 tty28
crw--w---- 1 root tty       4,  29 Oct  2 15:21 tty29
crw--w---- 1 root tty       4,   3 Oct  2 15:21 tty3
crw--w---- 1 root tty       4,  30 Oct  2 15:21 tty30
crw--w---- 1 root tty       4,  31 Oct  2 15:21 tty31
crw--w---- 1 root tty       4,  32 Oct  2 15:21 tty32
crw--w---- 1 root tty       4,  33 Oct  2 15:21 tty33
crw--w---- 1 root tty       4,  34 Oct  2 15:21 tty34
crw--w---- 1 root tty       4,  35 Oct  2 15:21 tty35
crw--w---- 1 root tty       4,  36 Oct  2 15:21 tty36
crw--w---- 1 root tty       4,  37 Oct  2 15:21 tty37
crw--w---- 1 root tty       4,  38 Oct  2 15:21 tty38
crw--w---- 1 root tty       4,  39 Oct  2 15:21 tty39
crw--w---- 1 root tty       4,   4 Oct  2 15:21 tty4
crw--w---- 1 root tty       4,  40 Oct  2 15:21 tty40
crw--w---- 1 root tty       4,  41 Oct  2 15:21 tty41
crw--w---- 1 root tty       4,  42 Oct  2 15:21 tty42
crw--w---- 1 root tty       4,  43 Oct  2 15:21 tty43
crw--w---- 1 root tty       4,  44 Oct  2 15:21 tty44
crw--w---- 1 root tty       4,  45 Oct  2 15:21 tty45
crw--w---- 1 root tty       4,  46 Oct  2 15:21 tty46
crw--w---- 1 root tty       4,  47 Oct  2 15:21 tty47
crw--w---- 1 root tty       4,  48 Oct  2 15:21 tty48
crw--w---- 1 root tty       4,  49 Oct  2 15:21 tty49
crw--w---- 1 root tty       4,   5 Oct  2 15:21 tty5
crw--w---- 1 root tty       4,  50 Oct  2 15:21 tty50
crw--w---- 1 root tty       4,  51 Oct  2 15:21 tty51
crw--w---- 1 root tty       4,  52 Oct  2 15:21 tty52
crw--w---- 1 root tty       4,  53 Oct  2 15:21 tty53
crw--w---- 1 root tty       4,  54 Oct  2 15:21 tty54
crw--w---- 1 root tty       4,  55 Oct  2 15:21 tty55
crw--w---- 1 root tty       4,  56 Oct  2 15:21 tty56
crw--w---- 1 root tty       4,  57 Oct  2 15:21 tty57
crw--w---- 1 root tty       4,  58 Oct  2 15:21 tty58
crw--w---- 1 root tty       4,  59 Oct  2 15:21 tty59
crw--w---- 1 root tty       4,   6 Oct  2 15:21 tty6
crw--w---- 1 root tty       4,  60 Oct  2 15:21 tty60
crw--w---- 1 root tty       4,  61 Oct  2 15:21 tty61
crw--w---- 1 root tty       4,  62 Oct  2 15:21 tty62
crw--w---- 1 root tty       4,  63 Oct  2 15:21 tty63
crw--w---- 1 root tty       4,   7 Oct  2 15:21 tty7
crw--w---- 1 root tty       4,   8 Oct  2 15:21 tty8
crw--w---- 1 root tty       4,   9 Oct  2 15:21 tty9
crw-rw---- 1 root dialout   4,  64 Oct  2 15:21 ttyS0
crw-rw---- 1 root dialout   4,  65 Oct  2 15:21 ttyS1
crw-rw---- 1 root dialout   4,  66 Oct  2 15:21 ttyS2
crw-rw---- 1 root dialout   4,  67 Oct  2 15:21 ttyS3
crw------- 1 root root     10, 239 Oct  2 15:21 uhid
crw------- 1 root root     10, 223 Oct  2 15:21 uinput
crw-rw-rw- 1 root root      1,   9 Oct  2 15:21 urandom
crw------- 1 root root    246,   0 Oct  2 15:21 usbmon0
crw------- 1 root root    246,   1 Oct  2 15:21 usbmon1
crw------- 1 root root    246,   2 Oct  2 15:21 usbmon2
crw-rw---- 1 root tty       7,   0 Oct  2 15:21 vcs
crw-rw---- 1 root tty       7,   1 Oct  2 15:21 vcs1
crw-rw---- 1 root tty       7,   2 Oct  2 15:21 vcs2
crw-rw---- 1 root tty       7,   3 Oct  2 15:21 vcs3
crw-rw---- 1 root tty       7,   4 Oct  2 15:21 vcs4
crw-rw---- 1 root tty       7,   5 Oct  2 15:21 vcs5
crw-rw---- 1 root tty       7,   6 Oct  2 15:21 vcs6
crw-rw---- 1 root tty       7, 128 Oct  2 15:21 vcsa
crw-rw---- 1 root tty       7, 129 Oct  2 15:21 vcsa1
crw-rw---- 1 root tty       7, 130 Oct  2 15:21 vcsa2
crw-rw---- 1 root tty       7, 131 Oct  2 15:21 vcsa3
crw-rw---- 1 root tty       7, 132 Oct  2 15:21 vcsa4
crw-rw---- 1 root tty       7, 133 Oct  2 15:21 vcsa5
crw-rw---- 1 root tty       7, 134 Oct  2 15:21 vcsa6
drwxr-xr-x 2 root root          60 Oct  2 15:21 vfio
crw------- 1 root root     10,  63 Oct  2 15:21 vga_arbiter
crw------- 1 root root     10, 137 Oct  2 15:21 vhci
crw------- 1 root root     10, 238 Oct  2 15:21 vhost-net
crw------- 1 root root     10, 241 Oct  2 15:21 vhost-vsock
crw------- 1 root root     10,  61 Oct  2 15:21 vmci
crw-rw-rw- 1 root root      1,   5 Oct  2 15:21 zero
在线举报