zoukankan      html  css  js  c++  java
  • Linux系统运维与架构设计之文件管理

    Linux系统运维与架构设计之文件管理

    4.1 Linux文件系统和目录结构

    4.1.1 Linux文件系统

    Linux文件系统中的文件和目录被组织成一个单根倒置树结构,文件系统从根目录下开始,使用"/"表示,而且Linux文件系统(CentOS6.10默认是ext4,CentOS7.6默认是xfs)的文件名区分大小写,但是有个需要注意的地方,如果CentOS系统上挂载了个U盘,而它的格式通常是VFAT,访问这个U盘的内容不区分大小写。因此是否区分大小写,和文件系统有关,和操作系统无关。

    要想查看磁盘的文件系统类型,可以使用lsblk -o +FSTYPE 命令查看

    CentOS7.6磁盘的文件系统查看

    [root@centos7 ~]#lsblk -o +FSTYPE
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT                      FSTYPE
    sda      8:0    0  200G  0 disk                                 
    ├─sda1   8:1    0  500M  0 part /boot                           xfs
    ├─sda2   8:2    0   50G  0 part /                               xfs
    ├─sda3   8:3    0   30G  0 part /data                           xfs
    ├─sda4   8:4    0    1K  0 part                                 
    └─sda5   8:5    0    4G  0 part [SWAP]                          swap
    sr0     11:0    1   10G  0 rom  /run/media/root/CentOS 7 x86_64 iso9660

    CentOS6.10磁盘的文件系统查看

    [root@centOS6 cd]#lsblk -o +FSTYPE
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT               FSTYPE
    sr0     11:0    1  3.7G  0 rom  /media/CentOS_6.10_Final iso9660
    sda      8:0    0  200G  0 disk                          
    ├─sda1   8:1    0    1G  0 part /boot                    ext4
    ├─sda2   8:2    0 48.8G  0 part /                        ext4
    ├─sda3   8:3    0 29.3G  0 part /data                    ext4
    ├─sda4   8:4    0    1K  0 part                          
    └─sda5   8:5    0  3.9G  0 part [SWAP]                   swap

    在Linux系统中,以.开头的文件表示隐藏文件,例如之前修改过的.bash_history和.bashrc等等都是隐藏文件。
    Linux文件系统对文件名的后缀没有特殊的要求,而Windows文件系统的可执行程序的后缀必须是.exe或者.msi。

    在Linux下的路径分隔使用正斜线(/)分隔,而Linux文件系统的路径主要有相对路径和绝对路径两种,其中相对路径是相对于当前路径开始的路径,而绝对路径是从根路径下开始,和路径相关的还有两个特殊的符号,其中..表示上一级目录,而./表示当前目录,相对路径相比绝对路径而言会更加灵活,在后期编写脚本程序时应该优先使用相对路径。

    使用相对路径查看/etc目录下的motd文件

    [root@centOS6 ~]#pwd #首先查看当前用户所在的路径
    /root
    [root@centOS6 ~]#cat ../etc/motd 
    欢迎跟光磊一起学习Linux系统运维与架构设计

    使用绝对路径查看/etc目录下的motd文件

    [root@centOS6 ~]#cat /etc/motd 
    欢迎跟光磊一起学习Linux系统运维与架构设计

    使用相对路径查看/etc目录下的motd文件

    [root@centOS6 ~]#cat ../etc/motd 
    欢迎跟光磊一起学习Linux系统运维与架构设计

    如果只要路径的文件或者文件夹,可以使用basename获取,如果只想要获取文件或者文件夹所在的路径,可以使用dirname获取

    [root@centOS6 ~]#basename /etc/sysconfig/network
    network
    [root@centOS6 ~]#dirname /etc/sysconfig/network
    /etc/sysconfig

    在Linux文件系统中,每个文件都有相应的数据,包含文件属性数据(例如大小,修改时间,权限信息等等),也被称为元数据(metadata)以及文件存放的内容本身的数据两部分组成,使用ll命令就可以查看文件的属性数据,如下命令输出显示。

    [root@centOS6 ~]#ll /etc/profile
    -rw-r--r--. 1 root root 1841 Mar 22  2017 /etc/profile

    在Linux文件系统中,不同类型的文件使用不同的颜色来区分,在/etc目录下的DIR_COLORS文件中定义了各个类型的文件所对应的颜色,可以使用vim /etc/DIR_COLORS命令查看,如果想要修改文件的颜色,只需要修改该文件即可。

    [root@centos7 bin]#vim /etc/DIR_COLORS

    而常见的文件类型如下列表所示

    • 普通文件(-):即文本文件
    • 目录文件(d):文件夹
    • 块设备文件(b):磁盘
    • 字符设备文件(c):
    • 符号链接文件(l):快捷方式
    • 管道文件(p)
    • 套接字文件(s)

    颜色对应的文件类型如下表格所示

    颜色 文件类型
    蓝色 目录
    绿色 可执行文件
    红色 压缩文件
    浅蓝色 链接文件
    灰色 其他文件

    通常运维人员打交道最多的是普通文件和目录文件

    4.1.2 Linux文件系统的文件名规则

    Linux文件系统中文件名最长只能是255个字节以内,如果在创建文件时,长度超过255个字节会提示 File name too long,而且包含路径在内的文件名最长为4095个字节。

    Linux系统的文件名不能使用空格,斜杆和NUL,也极不推荐使用命令选项作为文件名。如果在工作中遇到了垃圾的文件无法正常使用rm -f命令加上相对路径删除,只需要加上垃圾文件的绝对路径就可以删除。

    4.1.3 FHS

    Linux文件系统的目录结构和Windows截然不同,由文件系统层次结构标准组(Firesystem Hierarchy Standard Group )发布的文件系统层次结构标准明确规定了Linux文件系统的各个目录的结构,目前最新的版本是2004年1月29日发布的2.3版本,通过浏览文件系统层次结构标准的pdf文件,可以了解到Linux系统中每个文件的作用,目录实际上也是当作一种文件。

    4.1.4 Linux文件系统的目录结构

    当我们登录系统之后,默认会在家目录,例如普通用户的家目录一般是/home/USERNAME,而管理员用户的家目录是在/root目录下,而这个/就是Linux系统的根目录,相比Windows而言是以某个盘符(例如C盘或者D盘)作为根目录。

    我们可以使用pwd命令查看登录系统后用户所在的目录。
    root用户

    [root@centOS6 ~]#pwd
    /root

    普通用户

    [guanglei@centOS6 ~]$pwd 
    /home/guanglei

    而在根目录下有些重要的目录,这里我们使用tree命令来查看。
    系统安装以后,默认是不带tree命令,需要我们加载光盘镜像后自己安装,安装之前需要确保系统镜像已经被加载,以CentOS7.6为例,如下图所示
    CentOS7.6加载镜像
    CentOS7.6系统可以使用如下命令安装tree

    [root@centos7 media]#rpm -ivh /run/media/root/CentOS 7 x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm 
    

    而CentOS6.10系统可以使用如下命令安装tree

    [root@centOS6 ~]#rpm -ivh /media/CentOS_6.10_Final/Packages/tree-1.5.3-3.el6.x86_64.rpm 
    

    然后使用tree -L 1 / 命令就可以查看到根目录下的一级目录

    CentOS6.7根目录下的一级子目录如下命令输出所示

    [root@centOS6 ~]#tree -L 1 /
    /
    ├── bin
    ├── boot
    ├── data
    ├── dev
    ├── etc
    ├── home
    ├── lib
    ├── lib64
    ├── lost+found
    ├── media
    ├── misc
    ├── mnt
    ├── net
    ├── opt
    ├── proc
    ├── root
    ├── sbin
    ├── selinux
    ├── srv
    ├── sys
    ├── tmp
    ├── usr
    └── var

    CentOS7.6系统的根目录下一级目录如下命令输出显示

    [root@centos7 ~]#tree -L 1 /
    /
    ├── bin -> usr/bin
    ├── boot
    ├── data
    ├── dev
    ├── etc
    ├── home
    ├── lib -> usr/lib
    ├── lib64 -> usr/lib64
    ├── media
    ├── mnt
    ├── opt
    ├── proc
    ├── root
    ├── run
    ├── sbin -> usr/sbin
    ├── srv
    ├── sys
    ├── tmp
    ├── usr
    └── var
    
    20 directories, 0 files

    两者根目录的一级子目录直接的对比,如下图所示
    CentOS7.6和CentOS6.10根目录下一级子目录对比
    接下来就介绍根目录下的一级子目录及其作用,如下表格所示,其中标记 *的是FHS规定Linux系统中必须具备的目录。

    目录名称 主要作用
    /bin * 供所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
    /boot * 引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader, grub)都存放于此目录,需要独立分区,如果损坏,系统无法启动
    /dev * 设备文件以及特殊文件存储位置,例如块设备以及字符设备
    /sbin * 管理类(针对root用户)的基本命令;不能关联至独立分区, OS启动即会用到的程序
    /etc * 配置文件目录,存放应程序的相关配置
    /lib * 启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules)
    /home/USERNAME 普通用户的家目录
    /root 管理员的家目录
    /media * 便携式移动设备挂载点
    /mnt * 临时文件系统挂载点
    /var * 存放系统和软件运行的日志信息
    /opt * 第三方应用程序的安装位置
    /usr * 存放应用程序和文件
    /proc 用于输出内核与进程信息相关的虚拟文件系统
    /sys 用于输出当前系统上硬件设备相关信息虚拟文件系统
    /srv * 系统上运行的服务用到的数据
    /tmp * 临时文件存储位置
    /selinux security enhanced Linux, selinux相关的安全策略等信息的存储位置

    /boot目录
    这里使用ls -al /boot查看/boot目录下的文件列表,其中 vmlinuz-3.10.0-957.el7.x86_64就是Linux的内核,CentOS7.x的内核是3.10版本,如下命令执行输出结果显示。

    [root@centos7 ~]#ls -al /boot
    total 128912
    dr-xr-xr-x.  5 root root     4096 Jan 10 10:10 .
    dr-xr-xr-x. 18 root root      236 Jan 10 10:08 ..
    -rw-r--r--.  1 root root   151918 Nov  9 07:43 config-3.10.0-957.el7.x86_64
    drwx------.  3 root root       17 Nov  9 08:46 efi
    drwxr-xr-x.  2 root root       27 Jan 10 10:03 grub
    drwx------.  5 root root       97 Jan 10 10:09 grub2
    -rw-------.  1 root root 74000668 Jan 10 10:07 initramfs-0-rescue-40316785f67f494a9a258e83fd0d87a4.img
    -rw-------.  1 root root 29363228 Jan 10 10:09 initramfs-3.10.0-957.el7.x86_64.img
    -rw-------.  1 root root 11324650 Jan 10 10:10 initramfs-3.10.0-957.el7.x86_64kdump.img
    -rw-r--r--.  1 root root   314036 Nov  9 07:43 symvers-3.10.0-957.el7.x86_64.gz
    -rw-------.  1 root root  3543471 Nov  9 07:43 System.map-3.10.0-957.el7.x86_64
    -rwxr-xr-x.  1 root root  6639904 Jan 10 10:07 vmlinuz-0-rescue-40316785f67f494a9a258e83fd0d87a4
    -rwxr-xr-x.  1 root root  6639904 Nov  9 07:43 vmlinuz-3.10.0-957.el7.x86_64
    -rw-r--r--.  1 root root      166 Nov  9 07:43 .vmlinuz-3.10.0-957.el7.x86_64.hmac

    /dev目录
    这里使用ll命令查看/dev下的设备列表
    主要设备分为块设备和字符设备
    其中块设备特点是物理存在的,支持随机访问,支持缓存。
    字符设备是不带缓存,是逻辑的概念,不支持随机访问,也就是顺序访问。
    而且块设备和字符设备都没有大小的属性,只有设备的类别以及编号。

    查看/dev下的磁盘分区

    [root@centos7 ~]#ll /dev/sd*
    brw-rw----. 1 root disk 8, 0 Jan 29 23:05 /dev/sda
    brw-rw----. 1 root disk 8, 1 Jan 29 23:05 /dev/sda1
    brw-rw----. 1 root disk 8, 2 Jan 29 23:05 /dev/sda2
    brw-rw----. 1 root disk 8, 3 Jan 29 23:05 /dev/sda3
    brw-rw----. 1 root disk 8, 4 Jan 29 23:05 /dev/sda4
    brw-rw----. 1 root disk 8, 5 Jan 29 23:05 /dev/sda5

    /etc目录
    存放应用,服务的相关配置文件,后期运维工作会经常访问该目录来修改相应的配置文件(例如之前修改过的/etc/profile /etc/gdm/custom.conf /etc/profile.d/prompt.sh
    ),这里我们要养成一个良好的运维习惯,就是生产环境上在修改配置文件之前先备份,防止某些误操作造成无法挽回的损失。

    /lib目录
    这里使用ldd命令查看应用程序所依赖的系统库文件。
    Linux系统的命令都会依赖/lib或者/lib64(CentOS7都是64位)路径下的系统库。
    我们可以使用ldd命令跟上命令的路径就可以查看命令所依赖的库。

    [root@centos7 ~]#which ls #查看ls命令的路径
    alias ls='ls --color=auto'
            /usr/bin/ls
    [root@centos7 ~]#ldd /usr/bin/ls
            linux-vdso.so.1 =>  (0x00007fffe2927000)
            libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f3daeb99000)
            libcap.so.2 => /lib64/libcap.so.2 (0x00007f3dae994000)
            libacl.so.1 => /lib64/libacl.so.1 (0x00007f3dae78b000)
            libc.so.6 => /lib64/libc.so.6 (0x00007f3dae3be000)
            libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f3dae15c000)
            libdl.so.2 => /lib64/libdl.so.2 (0x00007f3dadf58000)
            /lib64/ld-linux-x86-64.so.2 (0x00007f3daedc0000)
            libattr.so.1 => /lib64/libattr.so.1 (0x00007f3dadd53000)
            libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3dadb37000)
            
    [root@centos7 ~]#which --skip-alias hostname
    /usr/bin/hostname
    [root@centos7 ~]#ldd /usr/bin/hostname #查看hostname依赖的系统库
            linux-vdso.so.1 =>  (0x00007ffd21f8d000)
            libnsl.so.1 => /lib64/libnsl.so.1 (0x00007f360227d000)
            libc.so.6 => /lib64/libc.so.6 (0x00007f3601eb0000)
            /lib64/ld-linux-x86-64.so.2 (0x00007f3602497000)

    /media和/mnt
    /media目录是当用户登录系统之后会自动挂载配置的镜像文件,而/mnt通常是管理员手动输入命令挂载。

    /bin和/sbin
    这里以CentOS6.10中的普通命令ls和管理员命令init为例说明/bin和/sbin目录的作用
    /bin目录下存放的是普通用户存放的命令,例如ls
    /sbin目录下存放的是管理员存放的命令,例如init

    [root@centOS6 ~]#which --skip-alias ls
    /bin/ls
    [root@centOS6 ~]#which init
    /sbin/init

    /usr目录
    /usr用于存放系统中的应用程序和文件,如果在安装系统时选择了很多软件包,那么这些软件包默认是安装在/usr路径下,平常安装的一些软件默认也是在/usr路径下,因此这个目录一般会比较大,可以使用du命令查看目录对应的磁盘空间,如下命令输出结果显示除了光盘镜像以外,/usr占据的磁盘空间是最大的。

    [root@centOS6 ~]#du -sh /*
    7.7M    /bin
    38M     /boot
    20K     /data
    312K    /dev
    41M     /etc
    214M    /home
    207M    /lib
    28M     /lib64
    16K     /lost+found
    3.8G    /media
    0       /misc
    8.0K    /mnt
    0       /net
    8.0K    /opt
    du: cannot access `/proc/4181/task/4181/fd/4': No such file or directory
    du: cannot access `/proc/4181/task/4181/fdinfo/4': No such file or directory
    du: cannot access `/proc/4181/fd/4': No such file or directory
    du: cannot access `/proc/4181/fdinfo/4': No such file or directory
    0       /proc
    1.2M    /root
    17M     /sbin
    0       /selinux
    4.0K    /srv
    0       /sys
    780K    /tmp
    3.1G    /usr
    83M     /var

    /proc目录
    /proc目录是一个虚拟目录,目录中所有的信息都是内存的映射,通过这个虚拟的内内存映射目录,可以获取CPU,内存,硬盘信息,/proc目录存放与内存中,而不是磁盘。

    如果想要查看CPU信息,可以使用cat cpuinfo命令

    [guanglei@centOS6 /]$cat /proc/cpuinfo  #查看CPU信息
    
    
    [guanglei@centOS6 /]$ll /proc/cpuinfo  #查看cpuinfo文件大小是0,因为存在内存中
    -r--r--r--. 1 root root 0 Jan 15 23:48 /proc/cpuinfo

    如果想要查看内存信息,可以使用cat meminfo命令查看,如下命令输出所示

    [guanglei@centOS6 /]$cat /proc/meminfo 
    MemTotal:        8045264 kB
    MemFree:         6520032 kB
    Buffers:           83236 kB
    Cached:           830312 kB
    SwapCached:            0 kB
    Active:           647804 kB
    Inactive:         429556 kB
    Active(anon):     163912 kB
    Inactive(anon):     4112 kB
    Active(file):     483892 kB
    Inactive(file):   425444 kB
    Unevictable:           0 kB
    Mlocked:               0 kB
    SwapTotal:       4095996 kB
    SwapFree:        4095996 kB
    Dirty:               140 kB
    Writeback:             0 kB
    AnonPages:        163808 kB
    Mapped:            66952 kB
    Shmem:              4220 kB
    Slab:             310788 kB
    SReclaimable:     222452 kB
    SUnreclaim:        88336 kB
    KernelStack:        7920 kB
    PageTables:        28240 kB
    NFS_Unstable:          0 kB
    Bounce:                0 kB
    WritebackTmp:          0 kB
    CommitLimit:     8118628 kB
    Committed_AS:     853068 kB
    VmallocTotal:   34359738367 kB
    VmallocUsed:      198680 kB
    VmallocChunk:   34359518624 kB
    HardwareCorrupted:     0 kB
    AnonHugePages:     34816 kB
    HugePages_Total:       0
    HugePages_Free:        0
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:       2048 kB
    DirectMap4k:        8192 kB
    DirectMap2M:     2088960 kB
    DirectMap1G:     6291456 kB

    如果想要查看磁盘分区信息,可以使用cat /proc/partitions 命令,如下命令输出所示
    当前的磁盘分区情况。

    [guanglei@centOS6 /]$cat /proc/partitions 
    major minor  #blocks  name
    
       8        0  209715200 sda
       8        1    1048576 sda1
       8        2   51200000 sda2
       8        3    4096000 sda3
       8        4          1 sda4
       8        5  153368576 sda5

    /sys目录
    /sys是用于存放系统硬件信息

    [root@centos7 ~]#ls -al /sys
    total 0
    dr-xr-xr-x.  13 root root   0 Jan 29 23:05 .
    dr-xr-xr-x.  18 root root 236 Jan 25 12:52 ..
    drwxr-xr-x.   2 root root   0 Jan 29 23:05 block
    drwxr-xr-x.  34 root root   0 Jan 29 23:05 bus
    drwxr-xr-x.  55 root root   0 Jan 29 23:05 class
    drwxr-xr-x.   4 root root   0 Jan 29 23:05 dev
    drwxr-xr-x.  16 root root   0 Jan 29 23:05 devices
    drwxr-xr-x.   6 root root   0 Jan 29 23:05 firmware
    drwxr-xr-x.   8 root root   0 Jan 29 23:05 fs
    drwxr-xr-x.   2 root root   0 Jan 29 23:05 hypervisor
    drwxr-xr-x.  10 root root   0 Jan 29 23:05 kernel
    drwxr-xr-x. 164 root root   0 Jan 29 23:05 module
    drwxr-xr-x.   2 root root   0 Jan 29 23:05 power

    这里使用/sys实现动态挂载硬盘,首先使用lsblk命令查看当前的磁盘以及分区信息,目前只有sda一块硬盘。

    [guanglei@centOS6 /]$lsblk
    NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1    55M  0 rom  
    sda      8:0    0   200G  0 disk 
    ├─sda1   8:1    0     1G  0 part /boot
    ├─sda2   8:2    0  48.8G  0 part /
    ├─sda3   8:3    0   3.9G  0 part [SWAP]
    ├─sda4   8:4    0     1K  0 part 
    └─sda5   8:5    0 146.3G  0 part /data

    我们可以在VMware Work Station中给CentOS6.10添加一个磁盘,然后使用如下命令让系统自动识别,该命令会触发磁盘扫描,让系统识别新增的磁盘。

    [root@centOS6 ~]#echo '- - -'> /sys/class/scsi_host/host2/scan

    再次使用lsblk命令查看新增加的磁盘信息,新增了一个100G的磁盘,默认未分区。

    [root@centOS6 ~]#lsblk
    NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0     11:0    1   3.7G  0 rom  /media/CentOS_6.10_Final
    sda      8:0    0   200G  0 disk 
    ├─sda1   8:1    0     1G  0 part /boot
    ├─sda2   8:2    0  48.8G  0 part /
    ├─sda3   8:3    0   3.9G  0 part [SWAP]
    ├─sda4   8:4    0     1K  0 part 
    └─sda5   8:5    0 146.3G  0 part /data
    sdb      8:16   0   100G  0 disk 

    /misc

    首先使用lsblk查看光盘的挂在情况
    当用户在图形界面登录系统后,光盘会自动挂在到/run/media/root目录下。
    如果开机时进入的是字符界面,则光盘不会自动挂载。

    [root@centos7 ~]#lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0  200G  0 disk 
    ├─sda1   8:1    0  500M  0 part /boot
    ├─sda2   8:2    0   50G  0 part /
    ├─sda3   8:3    0   30G  0 part /data
    ├─sda4   8:4    0    1K  0 part 
    └─sda5   8:5    0    4G  0 part [SWAP]
    sr0     11:0    1   10G  0 rom  /run/media/root/CentOS 7 x86_64

    针对上述情况,我们可以借助/misc目录实现光盘挂载。
    在CentOS7.6中,首先我们使用yum install -y autofs 安装autofs服务
    然后使用systemctl start autofs 实现开机启动autofs服务。

    [root@centos7 ~]#yum install -y autofs #安装autofs服务
    [root@centos7 ~]#systemctl start autofs #开机启动autofs服务

    然后使用ll /misc/cd 查看该目录下的内容

    [root@centos7 ~]#ll /misc/cd
    total 1656
    -rw-rw-r--. 1 root root      14 Nov 26 00:01 CentOS_BuildTag
    drwxr-xr-x. 3 root root    2048 Nov 26 00:20 EFI
    -rw-rw-r--. 1 root root     227 Aug 30  2017 EULA
    -rw-rw-r--. 1 root root   18009 Dec 10  2015 GPL
    drwxr-xr-x. 3 root root    2048 Nov 26 00:21 images
    drwxr-xr-x. 2 root root    2048 Nov 26 00:20 isolinux
    drwxr-xr-x. 2 root root    2048 Nov 26 00:20 LiveOS
    drwxrwxr-x. 2 root root 1656832 Nov 25 23:58 Packages
    drwxrwxr-x. 2 root root    4096 Nov 26 22:21 repodata
    -rw-rw-r--. 1 root root    1690 Dec 10  2015 RPM-GPG-KEY-CentOS-7
    -rw-rw-r--. 1 root root    1690 Dec 10  2015 RPM-GPG-KEY-CentOS-Testing-7
    -r--r--r--. 1 root root    2883 Nov 26 22:22 TRANS.TBL

    然后就可以通过访问/misc/cd/Packages/安装指定的软件包了

    [root@centos7 ~]#rpm -ivh /misc/cd/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm  #安装lrzsz实现文件上传下载

    在CentOS7.x以后,如下列表的目录都已经被合并了。

    • /bin和/usr/bin 合并为/usr/bin
    • /sbin和/usr/sbin合并为/usr/sbin
    • /lib和/usr/lib合并为/usr/lib
    • /lib64和/usr/lib64合并为/usr/lib64

    如下命令输出显示,/bin软链的形式指向/usr/bin

    [root@centos7 ~]#ll -d /bin /usr/bin
    lrwxrwxrwx. 1 root root     7 Jan 10 10:02 /bin -> usr/bin
    dr-xr-xr-x. 2 root root 49152 Jan 15 22:27 /usr/bin

    4.1.5 Linux应用程序的组成部分

    Linux应用程序由二进制程序以及依赖的库文件和配置文件以及帮助文件组成。
    通常二进制程序位于/bin,/sbin,/usr/bin,/usr/local/bin以及/usr/local/sbin目录下
    而库文件位于/lib,/lib64,/usr/lib,/usr/lib64,/usr/local/lib,/usr/local/lib64目录下
    配置文件位于/etc/及其子目录以及 /usr/local/etc下
    帮助文件位于/usr/share/man,/usr/share/doc,/user/local/share/man,/usr/local/share/doc目录下

    4.2 Linux文件管理

    4.2.1 pwd

    功能说明:pwd命令是显示当前工作目录,在日常运维工作中,我们会在不同的目录之间进行切换,当执行pwd命令后可以立刻得知当前工作目录的绝对路径。

    常用选项:

    • -L:显示软链接路径(默认)
    • -P:显示真实物理路径

    应用案例:

    [root@centos7 bin]#pwd #显示软链接路径
    /bin
    [root@centos7 bin]#pwd -L # pwd默认就是显示软连接路径
    /bin
    [root@centos7 bin]#pwd -P # 显示真实物理路径
    /usr/bin

    4.2.2 cd

    功能说明:cd(change directory)命令用于切换目录,在日常运维场景中,经常使用cd命令切换到不同的目录下执行不同的维护任务。
    cd是Shell自带的内置命令,在切换目录时可以使用TAB实现路径补全。

    [root@centos7 bin]#type cd
    cd is a shell builtin

    在切换目录时,还有一些特殊符号进入相关的目录

    符号名称 表示目录
    ~ 家目录
    ~guanglei 进入到用户给guanglei的家目录
    - 上一次目录
    .. 上一级目录

    应用案例:分别使用cd切换到管理员root用户家目录和普通用户guanglei的家目录

    [root@centos7 ~]#cd ~
    [root@centos7 ~]#pwd
    /root
    [root@centos7 ~]#cd ~guanglei
    [root@centos7 guanglei]#pwd
    /home/guanglei

    应用案例:使用cd实现切换到上级目录

    [root@centos7 guanglei]#cd ~
    [root@centos7 ~]#pwd
    /root
    [root@centos7 ~]#cd ..
    [root@centos7 /]#pwd
    /

    其中当前目录保存在$PWD环境变量中,而上一次目录的路径保存在$OLDPWD环境变量中,可以分别使用echo 查看它们的值

    [root@centos7 bin]#echo $PWD
    /bin
    [root@centos7 bin]#echo $OLDPWD
    /root

    如果某个目录是软连接,例如在CentOS7.6中,/bin目录就是软链接目录,cd默认切换的目录是软链接,而如果想要切换到真实的目录,可以通过-P选项实现。

    [root@centos7 bin]#cd /bin
    [root@centos7 bin]#pwd
    /bin
    [root@centos7 bin]#cd -P /bin
    [root@centos7 bin]#pwd
    /usr/bin

    如果想要获取一个文件的目录名称,可以使用dirname加上文件的绝对路径实现

    [root@centos7 bin]#dirname /etc/sysconfig/network-scripts/ifcfg-ens33 
    /etc/sysconfig/network-scripts

    如果想要获取一个目录下的文件名,可以使用basename加上文件的绝对路径实现

    [root@centos7 bin]#basename /etc/sysconfig/network-scripts/ifcfg-ens33 
    ifcfg-ens33

    4.2.3 ls

    功能说明:ls命令用于查看文件或者目录列表
    常用选项:

    选项名称 功能描述
    -a 显示目录下的所有文件,包含以.开头的隐藏文件
    -d 当遇到目录时,列出目录本身而非目录的文件,并且不跟随符号链接
    -F 在条目后加上文件类型的指示符号
    -h 以人类可读的信息显示文件或者目录的大小
    -i 显示文件节点编号
    -l 显示文件详细信息(包括文件的类型,权限,大小,所属用户,所属组别,文件内容最后一次修改时间,所属用户,文件类型),等价于ll
    -r 按照字典顺序反向排序
    -R 递归显示文件夹(包含子文件夹的内容)
    -S 按照文件从大到小排序
    -t 根据最后的修改时间(mtime)排序,默认是以文件名称排序
    -u 根据最后的访问时间(atime)排序
    -U 按照文件生成的先后顺序存放
    -c 根据最后的元数据修改时间(ctime)排序
    -X 按照文件的后缀排序
    -1 按照行显示目录,默认按照数字,字母排序

    如果先要了解ls的更多选项,可以使用ls --help查阅。

    使用ls查看文件列表时,默认使用的是别名,可以使用type查看ls命令的类型

    [root@centos7 ~]#type ls
    ls is aliased to `ls --color=auto'

    ,而别名的ls默认会给不同的文件类型增加颜色以示区分

    [root@centos7 ~]#ls
    anaconda-ks.cfg  Desktop  Documents  Downloads  helloworld.c  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos

    如果想要使用ls的原始命令,只需要在命令加上''即可

    [root@centos7 ~]#'ls'
    anaconda-ks.cfg  Desktop  Documents  Downloads  helloworld.c  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos

    如果想要查看指定目录下的所有文件,包含隐藏文件可以使用-a选项实现

    [root@centos7 ~]#ls -a
    .                .bash_history  .bashrc  .cshrc   Documents  helloworld.c          .local    Public     Videos
    ..               .bash_logout   .cache   .dbus    Downloads  .ICEauthority         Music     .tcshrc    .viminfo
    anaconda-ks.cfg  .bash_profile  .config  Desktop  .esd_auth  initial-setup-ks.cfg  Pictures  Templates

    ls中的-A选项,不会显示.和..

    [root@centos7 ~]#ls -a
    .                .bash_history  .bashrc  .cshrc   Documents  helloworld.c          .local    Public     Videos
    ..               .bash_logout   .cache   .dbus    Downloads  .ICEauthority         Music     .tcshrc    .viminfo
    anaconda-ks.cfg  .bash_profile  .config  Desktop  .esd_auth  initial-setup-ks.cfg  Pictures  Templates

    如果想要获取文件的属性信息,可以使用-l选项实现,也可以使用ll代替

    以文件anaconda-ks.cfg为例,其中-表示该文件为普通文本文件。
    Jan 25 12:53 表示该文件内容的最后修改日期(mtime)

    root表示文件所属的用户
    root表示文件所属的组

    [root@centos7 ~]#ls -l
    total 12
    -rw-------. 1 root root 1741 Jan 25 12:53 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Desktop
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Documents
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Downloads
    -rw-r--r--. 1 root root   97 Jan 29 15:39 helloworld.c
    -rw-r--r--. 1 root root 1772 Jan 25 12:56 initial-setup-ks.cfg
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Music
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Pictures
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Public
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Templates
    drwxr-xr-x. 2 root root    6 Jan 29 15:09 Videos

    如果修改文件的详细信息(例如这里修改文件的所有者),文件的修改时间是不会发生变化的

    [root@centos7 ~]#ll
    total 12
    -rw-------. 1 root     root 1741 Jan 25 12:53 anaconda-ks.cfg
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Desktop
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Documents
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Downloads
    -rw-r--r--. 1 guanglei root   97 Jan 29 15:39 helloworld.c
    -rw-r--r--. 1 root     root 1772 Jan 25 12:56 initial-setup-ks.cfg
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Music
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Pictures
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Public
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Templates
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Videos
    [root@centos7 ~]#chown guanglei helloworld.c  #修改文件的所属用户为guanglei
    [root@centos7 ~]#ll
    total 12
    -rw-------. 1 root     root 1741 Jan 25 12:53 anaconda-ks.cfg
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Desktop
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Documents
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Downloads
    -rw-r--r--. 1 guanglei root   97 Jan 29 15:39 helloworld.c
    -rw-r--r--. 1 root     root 1772 Jan 25 12:56 initial-setup-ks.cfg
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Music
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Pictures
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Public
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Templates
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Videos

    而文件的元数据修改时间使用ctime来记录,如果想要查看文件的元数据修改时间,可以使用ll --time=ctime实现

    [root@centos7 ~]#ll --time=ctime helloworld.c 
    -rw-r--r--. 1 guanglei root 97 Feb 12 10:45 helloworld.c

    这里通过重命名文件,实现修改文件的元数据,则ctime会发生改变

    [root@centos7 ~]#ll --time=ctime helloworld.c 
    -rw-r--r--. 1 guanglei root 97 Feb 12 10:45 helloworld.c
    [root@centos7 ~]#mv helloworld.c  hello.c
    [root@centos7 ~]#ll --time=ctime hello.c 
    -rw-r--r--. 1 guanglei root 97 Feb 12 10:50 hello.c

    当读取文件时,文件的访问时间(atime)会发生改变

    [root@centos7 ~]#ll --time=atime anaconda-ks.cfg 
    -rw-------. 1 root root 1741 Jan 25 12:55 anaconda-ks.cfg
    
    [root@centos7 ~]#cat anaconda-ks.cfg 
    [root@centos7 ~]#ll --time=atime anaconda-ks.cfg 
    -rw-------. 1 root root 1741 Feb 12 10:57 anaconda-ks.cfg

    atime更新的条件

    1. mtime的时间大于等于atime的时间
    2. atime的时间和当前时间相比超过一天以上。

    如果想要查看一个文件的atime,ctime和mtime,可以使用stat命令查看

    [root@centos7 ~]#stat hello.c 
      File: ‘hello.c’
      Size: 97              Blocks: 8          IO Block: 4096   regular file
    Device: 802h/2050d      Inode: 100922353   Links: 1
    Access: (0644/-rw-r--r--)  Uid: ( 1000/guanglei)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2019-02-12 10:52:15.094891199 +0800
    Modify: 2019-01-29 15:39:31.969925223 +0800
    Change: 2019-02-12 10:50:00.662896712 +0800
     Birth: -

    实际上记录atime没有太大的意义,还增加了磁盘IO,后期在网站运维时应该要考虑屏蔽atime。

    在使用ls查看文件列表时,不会直接看到目录下的子目录及其文件,如果想要查看,可以通过-R递归查看,如下命令输出显示。

    [root@centos7 ~]#ls -R /boot

    默认的ls是按照字典顺序(数字,字母)默认升序排序,如果想要倒序排序,可以使用-r选项实现,如下命令输出显示。

    [root@centos7 ~]#ls -r
    Videos  Templates  Public  Pictures  Music  initial-setup-ks.cfg  hello.c  Downloads  Documents  Desktop  anaconda-ks.cfg

    ls -l可以查看文件的元数据信息,但是如果想要查看指定目录的元数据信息,可以通过-d选项实现,如下命令输出显示。

    [root@centos7 ~]#ls -ld Desktop/
    drwxr-xr-x. 2 root root 6 Jan 29 15:09 Desktop/

    ls查看文件内容的时候,默认是横着显示的,如果想要按照每行显示,可以使用-1实现,如下命令输出显示了两者的对比。

    [root@centos7 ~]#ls
    anaconda-ks.cfg  Desktop  Documents  Downloads  hello.c  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
    [root@centos7 ~]#ls -1
    anaconda-ks.cfg
    Desktop
    Documents
    Downloads
    hello.c
    initial-setup-ks.cfg
    Music
    Pictures
    Public
    Templates
    Videos

    如果想要在查看文件列表时,按照从大到小排序,可以使用-S选项实现

    [root@centos7 ~]#ls -Sl
    total 12
    -rw-r--r--. 1 root     root 1772 Jan 25 12:56 initial-setup-ks.cfg
    -rw-------. 1 root     root 1741 Jan 25 12:53 anaconda-ks.cfg
    -rw-r--r--. 1 guanglei root   97 Jan 29 15:39 hello.c
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Desktop
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Documents
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Downloads
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Music
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Pictures
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Public
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Templates
    drwxr-xr-x. 2 root     root    6 Jan 29 15:09 Videos

    如果只想要查看目录下的文件夹,不会递归显示,可以使用ls -d */ 实现

    [root@centos7 ~]#ls -d */
    Desktop/  Documents/  Downloads/  Music/  Pictures/  Public/  Templates/  Videos/

    ls查看文件大小时默认是以字节的单位显示,如果想要计算成KB,MG或者GB,可以使用-h选项实现

    [root@centos7 ~]#cd /etc/
    [root@centos7 etc]#ls -lh

    4.2.4 文件通配符

    Linux提供了诸多的文件通配符来实现对文件的管理,常用的通配符如下表格所示

    通配符名称 功能
    * 匹配零个或者多个字符
    ? 匹配任意单个字符
    ~ 当前用户家目录
    ~guanglei 用户guanglei家目录
    ~+ 当前工作目录
    ~- 前一个工作目录
    [0-9] 匹配数字范围
    [a-z] 匹配字母范围
    [A-Z] 匹配字母范围
    [wang] 匹配列表中的任意字符
    [^wang] 匹配列表中的所有字符的以外字符

    应用案例:查看以.cfg结尾的文件

    [root@centos7 ~]#ls *.cfg
    anaconda-ks.cfg  initial-setup-ks.cfg

    应用案例:查看三个字符的文本文件
    字母和英文都是一个字符

    [root@centos7 ~]#cd /tmp
    [root@centos7 tmp]#touch abc.txt 刘光磊.txt
    [root@centos7 tmp]#ll ???.txt
    -rw-r--r--. 1 root root 0 Feb 16 10:05 abc.txt
    -rw-r--r--. 1 root root 0 Feb 16 10:05 刘光磊.txt

    应用案例: 查看当前用户家目录文件列表

    [root@centos7 tmp]#ls ~
    anaconda-ks.cfg  Documents  hello.c               Music     Public     test.sh
    Desktop          Downloads  initial-setup-ks.cfg  Pictures  Templates  Videos

    应用案例:查看当前工作目录文件列表

    [root@centos7 tmp]#ls ~+
    abc.txt
    ssh-54so54gWt0i3
    ssh-5a9O0v5VuZjN
    systemd-private-6d0a7a093d3747a7a364bdb0fcde2871-colord.service-OMvvKs
    systemd-private-6d0a7a093d3747a7a364bdb0fcde2871-fwupd.service-7cIBY8
    systemd-private-6d0a7a093d3747a7a364bdb0fcde2871-rtkit-daemon.service-9ntYps
    systemd-private-aa581b2066814a7abb99227727ade380-bolt.service-n8nqET
    systemd-private-aa581b2066814a7abb99227727ade380-colord.service-Gxx3OS
    systemd-private-aa581b2066814a7abb99227727ade380-cups.service-v2ZAhp
    systemd-private-aa581b2066814a7abb99227727ade380-fwupd.service-gRLg43
    systemd-private-aa581b2066814a7abb99227727ade380-rtkit-daemon.service-OzO7BR
    tracker-extract-files.0
    vmware-root_9047-4146505294
    vmware-root_9064-3100720561
    yum_save_tx.2019-02-11.10-12.oVUsni.yumtx
    yum_save_tx.2019-02-12.10-04.J3Asgo.yumtx
    yum_save_tx.2019-02-13.09-48.Ddee4t.yumtx
    yum_save_tx.2019-02-14.09-44.KRg3qP.yumtx
    yum_save_tx.2019-02-16.09-37.3vpThE.yumtx
    刘光磊.txt

    应用案例:查看上一个工作目录文件列表

    [root@centos7 tmp]#ls ~-
    anaconda-ks.cfg  Documents  hello.c               Music     Public     test.sh
    Desktop          Downloads  initial-setup-ks.cfg  Pictures  Templates  Videos

    应用案例:按照数字通配符匹配文件

    首先创建五个文件 file1,file2,file3,file4,file5

    [root@centos7 tmp]#touch file{1..5}
    [root@centos7 tmp]#ll file*
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file1
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file2
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file3
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file4
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file5

    然后查找file1-file5文件列表

    [root@centos7 tmp]#ll file[1-5]
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file1
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file2
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file3
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file4
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file5

    查找指定名字的文件列表

    [root@centos7 tmp]#ll file[1,3,5]
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file1
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file3
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file5

    应用案例:按照字母通配符查看文件列表

    首先创建测试数据 filea,fileb,filec,filed,filee,filef

    [root@centos7 tmp]#touch file{a..f}

    然后使用字母通配符查看文件列表

    [root@centos7 tmp]#ll file[af]
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filec
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filed
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filee
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef

    应用案例:数字和字母通配符的综合使用

    [root@centos7 tmp]#ll file[a-zA-Z0-9]
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file1
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file2
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file3
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file4
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file5
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filec
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filed
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filee
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef

    应用案例:[a-f]和[af]的区别

    首先创建测试文件fileA,fileB,fileC,fileD,fileE,fileF

    [root@centos7 tmp]#touch file{A..F}
    [root@centos7 tmp]#ll file*
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file1
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file2
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file3
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file4
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file5
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA
    -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filec
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filed
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filee
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileF

    然后比较两者的区别
    [af]表示匹配两个字符,[a-f]表示匹配区间,但是它也会匹配大写字母,按照小写字母,大写字母排序。

    [root@centos7 tmp]#ll file[af]
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
    [root@centos7 tmp]#ll file[a-f]
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA
    -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filec
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filed
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filee
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef

    如果想要实现
    预定义的字符类:

    字符类名称 功能
    [:digit:] 任意数字,相当于0-9
    [:lower:] 任意小写字母
    [:upper:] 任意大写字母
    [:alpha:] 任意大小写字母
    [:alnum:] 任意数字和字母

    应用案例:匹配文件名是file小写字母的文件列表

    [root@centos7 tmp]#ll file[[:lower:]]
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filec
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filed
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filee
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef

    应用案例:匹配文件名是file数字的文件列表

    [root@centos7 tmp]#ll file[[:digit:]]
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file1
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file2
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file3
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file4
    -rw-r--r--. 1 root root 0 Feb 16 10:08 file5

    应用案例:匹配文件名是file大写字母的文件列表

    [root@centos7 tmp]#ll file[[:upper:]]
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileF

    应用案例:排除文件名是file数字的文件列表

    [root@centos7 tmp]#ll file[^[:digit:]]
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filea
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileA
    -rw-r--r--. 1 root root 0 Feb 16 10:12 fileb
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileB
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filec
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileC
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filed
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileD
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filee
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileE
    -rw-r--r--. 1 root root 0 Feb 16 10:12 filef
    -rw-r--r--. 1 root root 0 Feb 16 10:19 fileF

    应用案例:显示指定目录下的隐藏文件

    [root@centos7 ~]#ls -d .[^.]*
    .bash_history  .bash_profile  .cache   .cshrc  .esd_auth      .local   .viminfo
    .bash_logout   .bashrc        .config  .dbus   .ICEauthority  .tcshrc

    如果想要了解更多通配符,可以使用man 7 glob命令查看

    [root@centos7 tmp]#man 7 glob

    ls和通配符的综合案例

    应用案例:显示/var目录下所有以l开头,以一个小写字母结尾,且中
    间出现至少一位数字的文件或目录

    [root@centos7 ~]#cd  /var/
    [root@centos7 var]#touch l8e
    [root@centos7 var]#touch lf8ga
    [root@centos7 var]#ll /var/l*[[:digit:]]*[[:lower:]]

    应用案例:显示/etc目录下以任意一位数字开头,且以非数字结尾的
    文件或目录

    [root@centos7 var]#ll -d /etc/[[:digit:]]*[^[:digit:]]

    应用案例:显示/etc/目录下以非字母开头,后面跟了一个字母及其
    它任意长度任意字符的文件或目录

    [root@centos7 var]#ll  /etc/[^[:alpha:]][[:alpha:]]* 

    应用案例:显示/etc/目录下所有以rc开头,并后面是0-6之间的数
    字,其它为任意字符的文件或目录

    [root@centos7 var]#ls -d /etc/rc[0-6]*
    /etc/rc0.d  /etc/rc1.d  /etc/rc2.d  /etc/rc3.d  /etc/rc4.d  /etc/rc5.d  /etc/rc6.d

    应用案例:显示/etc目录下,所有以.d结尾的文件或目录

    [root@centos7 var]#ls -d /etc/rc[0-6]*
    /etc/rc0.d  /etc/rc1.d  /etc/rc2.d  /etc/rc3.d  /etc/rc4.d  /etc/rc5.d  /etc/rc6.d

    应用案例:显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文
    件或目录

     [root@centos7 ~]#ll -d /etc/[mnrp]*.conf
    -rw-r--r--. 1 root root 5171 Oct 31 04:26 /etc/man_db.conf
    -rw-r--r--. 1 root root  936 Oct 31 03:03 /etc/mke2fs.conf
    -rw-r--r--. 1 root root 2620 Jun 10  2014 /etc/mtools.conf
    -rw-r--r--. 1 root root  967 Nov  8 00:56 /etc/nfs.conf
    -rw-r--r--. 1 root root 3391 Nov  8 00:56 /etc/nfsmount.conf
    -rw-r--r--. 1 root root 1746 Jan 25 12:47 /etc/nsswitch.conf
    -rw-r--r--. 1 root root   91 Dec  3  2012 /etc/numad.conf
    -rw-r--r--. 1 root root 1362 Jun 10  2014 /etc/pbm2ppa.conf
    -rw-r--r--. 1 root root 6300 Jun 10  2014 /etc/pnm2ppa.conf
    -rw-r--r--. 1 root root  433 Oct 31 01:54 /etc/radvd.conf
    -rw-r--r--. 1 root root 1787 Jun 10  2014 /etc/request-key.conf
    -rw-r--r--. 1 root root   71 Feb 16 09:37 /etc/resolv.conf
    -rw-r--r--. 1 root root  458 Apr 11  2018 /etc/rsyncd.conf
    -rw-r--r--. 1 root root 3232 Oct 30 22:49 /etc/rsyslog.conf

    应用案例:只显示/root下的隐藏文件和目录

    [root@centos7 ~]#ll -d /root/.[^.]*
    -rw-------.  1 root root 5750 Feb 14 21:00 /root/.bash_history
    -rw-r--r--.  1 root root   18 Dec 29  2013 /root/.bash_logout
    -rw-r--r--.  1 root root  176 Dec 29  2013 /root/.bash_profile
    -rw-r--r--.  1 root root  176 Dec 29  2013 /root/.bashrc
    drwx------. 13 root root  277 Jan 29 15:10 /root/.cache
    drwx------. 14 root root  261 Jan 29 15:10 /root/.config
    -rw-r--r--.  1 root root  100 Dec 29  2013 /root/.cshrc
    drwx------.  3 root root   25 Jan 25 12:55 /root/.dbus
    -rw-------.  1 root root   16 Jan 29 15:09 /root/.esd_auth
    -rw-------.  1 root root 1554 Feb 14 09:43 /root/.ICEauthority
    drwx------.  3 root root   19 Jan 29 15:09 /root/.local
    -rw-r--r--.  1 root root  129 Dec 29  2013 /root/.tcshrc
    -rw-------.  1 root root 6745 Jan 29 23:01 /root/.viminfo

    应用案例:只显示/etc下的非隐藏目录

    [root@centos7 ~]#ll -d /etc/[^.]*/

    4.2.5 touch

    功能说明:用于创建空文件或者创建已经存在的文件时会刷新文件的时间戳(atime,ctime,mtime)
    常用选项:

    选项名称 功能描述
    -a 改变atime和ctime
    -m 改变mtime和ctime
    -c 如果文件不存在,则不创建 ,即刷新时间戳
    -t 指定文件的atime和mtime时间戳

    应用案例: 创建一个脚本文件

    [root@centos7 ~]#touch test.sh
    [root@centos7 ~]#ll test.sh 
    -rw-r--r--. 1 root root 0 Feb 14 15:30 test.sh

    应用案例:修改文件的时间戳

    [root@centos7 ~]#stat test.sh 
      File: ‘test.sh’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: 802h/2050d      Inode: 100663381   Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2019-02-14 15:30:14.598261308 +0800
    Modify: 2019-02-14 15:30:14.598261308 +0800
    Change: 2019-02-14 15:30:14.598261308 +0800
     Birth: -
    [root@centos7 ~]#touch -a test.sh 
    [root@centos7 ~]#stat test.sh 
      File: ‘test.sh’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: 802h/2050d      Inode: 100663381   Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2019-02-14 15:31:39.975258276 +0800
    Modify: 2019-02-14 15:30:14.598261308 +0800
    Change: 2019-02-14 15:31:39.975258276 +0800
     Birth: -

    4.2.6 cp

    功能描述:用于普通文件文件或者目录的复制
    常用选项:

    选项名称 功能描述
    -i 覆盖存在的文件提示
    -n 不覆盖,注意-i和-n使用时,-i需要放在-n后面
    -r 递归复制目录以及目录下的所有内容
    -d 复制软连接 ,默认是复制的软连接指向的文件
    --preserv 复制文件保留指定的属性,例如timestamp,owership
    -a 相当于-dR -preserv=all ,复制文件时保留文件所有属性,实现归档
    -v 查看复制文件是时的详细些信息
    -u 只复制源比目标更新文件或者目标不存在的文件
    --backup=numberd 如果目标存在,覆盖前先备份后加上数字后缀

    应用案例:复制一个源文件到不存在的目录
    如果该目录下不存在,则会将源文件(例如这里的motd)改名之后复制到指定的目录中,原有的文件依然存在

    [root@centos7 ~]#cp /etc/motd /tmp/motd.bak
    [root@centos7 ~]#ll /tmp|grep motd.bak
    -rw-r--r--. 1 root root    60 Feb 16 11:11 motd.bak

    使用cat查看复制后源文件和目标文件的内容

    [root@centos7 ~]#cat /tmp/motd.bak 
    欢迎跟光磊一起学习Linux系统运维与架构设计
    [root@centos7 ~]#cat /etc/motd
    欢迎跟光磊一起学习Linux系统运维与架构设计

    应用案例:复制一个源文件到已经存在的目录
    如果没有使用任何选项,root用户默认会提示覆盖已经存在的文件(普通用户不会提示),因为root用户使用的是别名

    
    [root@centos7 ~]#alias cp
    alias cp='cp -i'

    如果想要覆盖文件,输入y则会覆盖。,此类文件复制有丢失文件数据的风险

    [root@centos7 ~]#cp /etc/issue /tmp/motd.bak 
    cp: overwrite ‘/tmp/motd.bak’? y

    使用cat查看复制后源文件和目标文件的内容

    [root@centos7 ~]#cat /etc/issue
    S
    Kernel 
     on an m
    
    [root@centos7 ~]#cat /tmp/motd.bak 
    S
    Kernel 
     on an m
    

    应用案例:复制源文件到指定的目录
    当目标位置是目录时,会将单个文件复制到目录中,并保留源文件名称。

    [root@centos7 ~]#cp /etc/issue /tmp
    [root@centos7 ~]#ll /tmp/issue 
    -rw-r--r--. 1 root root 23 Feb 16 11:24 /tmp/issue

    应用案例:当复制多个文件时,目标必须是文件夹,不能是不存在的文件或者文件夹,否则会提示错误

    [root@centos7 ~]#cp /etc/redhat-release  /etc/issue  /tmp/nofile
    cp: target ‘/tmp/nofile’ is not a directory

    应用案例:当复制的源文件是文件夹时,需要添加-r选项,而且目标文件必须要是文件夹,如果是不存在的文件夹则会发生重命名(即将root重命名为目标文件名)后复制。

    [root@centos7 ~]#cp -r /root/ /tmp/xxx
    [root@centos7 ~]#tree /tmp/xxx
    /tmp/xxx
    ├── anaconda-ks.cfg
    ├── Desktop
    ├── Documents
    ├── Downloads
    ├── hello.c
    ├── initial-setup-ks.cfg
    ├── Music
    ├── Pictures
    ├── Public
    ├── Templates
    ├── test.sh
    └── Videos
    
    8 directories, 4 files

    应用案例:将/etc/目录下所有文件备份到/testdir独立的子目录下, 并要求子目
    录格式为 backupYYYY-mm-dd, 备份过程可见

    [root@centos7 tmp]#cp -av /etc/ /tmp/backup`date +%F`
    [root@centos7 tmp]#ll /tmp/backup2019-02-16/

    应用案例创建/testdir/rootdir目录,并复制/root下所有
    文件到该目录内,要求保留原有权限

    [root@centos7 ~]#mkdir -p /testdir/rootdir
    [root@centos7 ~]#cp -r /root/ /testdir/rootdir/ --preserve=mode

    4.2.7 mv

    功能描述:移动或者重命名文件
    常用选项:

    选项名称 选项功能
    -i 交互式移动或者重命名
    -f 强制移动或者重命名

    应用案例:实现文件移动

    [root@centos7 tmp]#mkdir /tmp/source
    [root@centos7 tmp]#mkdir -p /tmp/target
    [root@centos7 tmp]#touch /tmp/source/test.txt
    [root@centos7 tmp]#mv /tmp/source/test.txt /tmp/target/
    [root@centos7 tmp]#ll /tmp/source/
    total 0
    [root@centos7 tmp]#ll /tmp/target/
    total 0
    -rw-r--r--. 1 root root 0 Feb 16 12:33 test.txt

    应用案例:实现文件重命名

    [root@centos7 tmp]#mv /tmp/target/test.txt  /tmp/target/test_new.txt
    [root@centos7 tmp]#ll /tmp/target/test_new.txt 
    -rw-r--r--. 1 root root 0 Feb 16 12:33 /tmp/target/test_new.txt

    应用案例:移动并重命名

    [root@centos7 tmp]#mv /tmp/target/test_new.txt  /tmp/source/test.txt
    [root@centos7 tmp]#ll /tmp/source/test.txt 
    -rw-r--r--. 1 root root 0 Feb 16 12:33 /tmp/source/test.txt

    应用案例:使用rename实现批量修改文件名

    首先在/tmp目录下创建10个测试文件

    [root@centos7 tmp]#touch file{1..10}.log
    [root@centos7 tmp]#ll
    total 0
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file10.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file1.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file2.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file3.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file4.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file5.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file6.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file7.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file8.log
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file9.log
    

    然后使用rename实现批量变更文件

    [root@centos7 tmp]#rename '.log' '.log.bak' file*
    [root@centos7 tmp]#ll
    total 0
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file10.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file1.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file2.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file3.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file4.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file5.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file6.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file7.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file8.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file9.log.bak
    [root@centos7 tmp]#rename 'file' 'file0' file*
    [root@centos7 tmp]#ll
    total 0
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file010.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file01.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file02.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file03.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file04.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file05.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file06.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file07.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file08.log.bak
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file09.log.bak

    4.2.8 rm

    功能说明:删除文件或者目录
    常用选项:

    功能名称 选项说明
    -f 强制删除
    -r 递归删除 ,用于删除目录
    -i 交互式删除

    应用案例: 自杀式删除,工作中禁止使用,CentOS7中已经被禁用了,除非加上--no-preserve-root长选项

    [root@centos7 tmp]#rm -rf /
    rm: it is dangerous to operate recursively on ‘/’
    rm: use --no-preserve-root to override this failsafe

    应用案例:定义别名,防止误删除

    [root@centos7 tmp]#mkdir -p /tmp/bak
    [root@centos7 tmp]#alias rm='mv -t /tmp/bak'
    [root@centos7 tmp]#rm /tmp/file0
    file010.log.bak  file02.log.bak   file04.log.bak   file06.log.bak   file08.log.bak   
    file01.log.bak   file03.log.bak   file05.log.bak   file07.log.bak   file09.log.bak   
    [root@centos7 tmp]#rm /tmp/file0
    file010.log.bak  file02.log.bak   file04.log.bak   file06.log.bak   file08.log.bak   
    file01.log.bak   file03.log.bak   file05.log.bak   file07.log.bak   file09.log.bak   
    [root@centos7 tmp]#rm /tmp/file01.log.bak 
    [root@centos7 tmp]#ll /tmp/bak/
    total 0
    -rw-r--r--. 1 root root 0 Feb 16 12:44 file01.log.bak

    在删除大文件时,如果文件依然在使用则不会立即释放空间。
    这时可以使用>实现清空文件内容,而不推荐使用rm。

    4.2.9 tree

    功能描述:显示指定的目录树
    常用选项:

    选项名称 功能描述
    -d 只显示目录
    -L 显示指定的层级数目
    -P 只显示由-P 匹配到的路径

    应用案例:显示根目录下的一级子目录

    
    8 directories, 4 files
    [root@centos7 ~]#tree -L 1 /
    /
    ├── bin -> usr/bin
    ├── boot
    ├── data
    ├── dev
    ├── etc
    ├── home
    ├── lib -> usr/lib
    ├── lib64 -> usr/lib64
    ├── media
    ├── misc
    ├── mnt
    ├── net
    ├── opt
    ├── proc
    ├── root
    ├── run
    ├── sbin -> usr/sbin
    ├── srv
    ├── sys
    ├── testdir
    ├── tmp
    ├── usr
    └── var
    
    23 directories, 0 files

    应用案例:显示指定文件夹的目录树

    [root@centos7 ~]#tree -P /etc/sysconfig/
    .
    ├── Desktop
    ├── Documents
    ├── Downloads
    ├── Music
    ├── Pictures
    ├── Public
    ├── Templates
    └── Videos
    
    8 directories, 0 files

    应用案例:显示root用户的文件夹目录树

    [root@centos7 ~]#tree -d ~
    /root
    ├── Desktop
    ├── Documents
    ├── Downloads
    ├── Music
    ├── Pictures
    ├── Public
    ├── Templates
    └── Videos
    
    8 directories

    4.2.10 mkdir

    功能描述:用于创建目录
    常用选项:

    选项名称 功能描述
    -p 创建父目录
    -v 查看创建过程

    应用案例:创建多级目录

    [root@centos7 ~]#mkdir -p Desktop/parent/child

    应用案例:显示创建目录的过程

    [root@centos7 ~]#mkdir -pv Desktop/china/shanghai
    mkdir: created directory ‘Desktop/china’
    mkdir: created directory ‘Desktop/china/shanghai’

    应用案例:创建多个文件夹1

    [root@centos7 Desktop]#cd /tmp/
    [root@centos7 tmp]#mkdir -pv /testdir/dir1/{x,y}/{a,b}
    mkdir: created directory ‘/testdir/dir1’
    mkdir: created directory ‘/testdir/dir1/x’
    mkdir: created directory ‘/testdir/dir1/x/a’
    mkdir: created directory ‘/testdir/dir1/x/b’
    mkdir: created directory ‘/testdir/dir1/y’
    mkdir: created directory ‘/testdir/dir1/y/a’
    mkdir: created directory ‘/testdir/dir1/y/b’

    应用案例:创建多个文件夹2

    [root@centos7 tmp]#mkdir -pv /testdir/dir2/{x/{a,b},y}
    mkdir: created directory ‘/testdir/dir2’
    mkdir: created directory ‘/testdir/dir2/x’
    mkdir: created directory ‘/testdir/dir2/x/a’
    mkdir: created directory ‘/testdir/dir2/x/b’
    mkdir: created directory ‘/testdir/dir2/y’

    应用案例:创建多个文件夹3

    [root@centos7 testdir]#mkdir -pv /testdir/dir{3,4,5/dir{6,7}}
    mkdir: created directory ‘/testdir/dir3’
    mkdir: created directory ‘/testdir/dir4’
    mkdir: created directory ‘/testdir/dir5’
    mkdir: created directory ‘/testdir/dir5/dir6’
    mkdir: created directory ‘/testdir/dir5/dir7’

    4.2.11 rmdir

    功能描述:删除空目录,如果目录下有内容是无法删除的。
    常用选项:

    选项名称 功能描述
    -p 删除父目录

    应用案例:删除子目录

    [root@centos7 ~]#rmdir Desktop/china/shanghai/
    [root@centos7 ~]#ll Desktop/china/
    total 0

    应用案例:删除多级子目录

    [root@centos7 Desktop]#mkdir -p parent/child
    [root@centos7 Desktop]#rmdir -p parent/child/
    [root@centos7 Desktop]#ll
    total 0
    drwxr-xr-x. 2 root root 6 Feb 16 13:11 china
  • 相关阅读:
    Mybatis逆向工程构建项目实例.
    JVM调优总结 -Xms -Xmx -Xmn -Xss
    mvn test 执行testng测试用例
    jmeter 发送http请求,并把获取到的请求的订单信息保存到文件中
    jenkins job构建后汇总结果到同一个文本文档中去
    shell 批量查看job 配置
    jenkins 发送邮件模板
    jenkins 发送邮件失败
    maven 私服中央库使用阿里云库
    jenkins 下载插件失败处理办法
  • 原文地址:https://www.cnblogs.com/ittimeline/p/10387654.html
Copyright © 2011-2022 走看看