linux老男孩 基础部分章节06-----操作系统文件属性知识

 

 

day13-操作系统文件属性知识    

1. 操作系统文件属性概念

    [root@oldboyedu ~]# ls -li /etc/
    total 1092
                          第一个部分:文件属性               第二个部分:文件名称
     34141705 d rwxr-xr-x.  3 root root      101 Mar 28 10:42 abrt
     34022854 - rw-r--r--.  1 root root       16 Mar 28 10:45 adjtime
     33590855 - rw-r--r--.  1 root root     1518 Jun  7  2013 aliases
     33554501 - rw-r--r--.  1 root root    12288 Mar 28 10:48 aliases.db
     33583487 d rwxr-xr-x.  2 root root      236 Mar 28 10:42 alternatives
     34105318 - rw-------.  1 root root      541 Apr 11  2018 anacrontab
     33862777 - rw-r--r--.  1 root root       55 Apr 11  2018 asound.conf
     34193678 d rwxr-x---.  3 root root       43 Mar 28 10:42 audisp
    101181822 d rwxr-x---.  3 root root       83 Mar 28 10:48 audit
       01     02  03       04 05   06         07    08

01. 文件数据的inode信息: index node == 索引节点  
        作用: 快递从磁盘中找到数据信息
02. 文件的数据类型: 文件 目录 链接文件 设备文件
        PS: linux系统中一切皆文件
        作用: 指明数据的文件类型
03. 权限位信息: r ead  读  w rite 写  execute 执行
        作用: 控制让不同的用户对文件可以有不同的权限
04. 文件目录的硬链接数: 就类似于超市的多个门
        作用: 可以多个路径查看数据信息
05. 文件所属用户信息(属主): 数据(孩子小于16岁) (属主)监护人
        作用: 文件的创建或者拥有者
06. 文件所属组信息(属组):   数据(孩子小于16岁) (属主)监护人父亲 (属组)亲人 (隔壁老王)其他用户
        作用: 文件或数据的所属用户组
07. 文件的大小信息
08. 文件的时间信息

权限位后面有个.   开启了SELinux功能的Linux系统才会有这个点

[root@xue/home/xuezy]# getenforce
Enforcing
[root@xue/home/xuezy]# setenforce  0
[root@xue/home/xuezy]# getenforce
Permissive

vi /etc/sysconfig/selinux
SELINUX=disabled 

[root@localhost ~]# /usr/sbin/sestatus -v
SELinux status:                 disabled


# ll
-rwxr-xr--  root root    #没有selinux上下文,没有ACL
-rwx--xr-x+ root root    #只有ACL,没有selinux上下文
-rw-r--r--. root root    #只有selinux上下文,没有ACL
-rwxrwxr--+ root root    #有selinux上下文,有ACL

2. 文件属性详细说明--文件类型

   1. 文件类型概念
    windows: 区分文件的类型 扩展名
    linux:   不用扩展名来区分文件类型
    
    2. linux系统区分文件类型方法
    a. 颜色区分
    b. 利用属性信息区分

      ls -l 
      d rwxr-xr-x. 2 root root   6 Apr 12 11:19 oldboy.jpg
      - rw-r--r--. 1 root root  35 Apr 12 08:52 oldboy.txt

    c .利用特殊命令区分

      [root@oldboyedu ~]# file /oldboy/oldboy.jpg
      /oldboy/oldboy.jpg: directory
      [root@oldboyedu ~]# file /oldboy/oldboy.txt
      /oldboy/oldboy.txt: ASCII text

   3. linux系统中的常见文件类型

       *d   -- 目录文件类型
       *-   -- 普通文件 
            ①. 纯文本文件 text /etc/hosts        --- 文本文件(可以直接编辑查看的普通文件)
            ②. 二进制文件 binary /bin/ls          --- 命令文件(不可以编辑)
            ③. 数据型文件 data /tmp/etc.tar.gz    --- 数据文件(压缩的文件)
       *l   -- 链接文件(快捷方式)
            创建方法:

 ln -s /oldboy/oldboy.txt  /oldboy/oldboy_link.txt  
        源文件             快捷方式(软链接文件)

        c/b -- 块文件/字符文件(设备文件)
              块文件: 存储的设备文件  光驱设备文件 磁盘
              字符文件: 根本停不下来的输出字符信息
       
              磁盘设备文件 /dev/sda          磁盘文件
              随机设备文件 /dev/urandom      
              输出设备文件 /dev/zero         黑洞出口文件???  磁盘分区
              输入设备文件 /dev/null         黑洞入口文件???  定时任务
              
       s   -- socket文件(网络编程)

4. 扩展命令说明

a 显示文件信息类型命令: file

      [root@oldboyedu ~]# file /oldboy/oldboy.jpg
      /oldboy/oldboy.jpg: directory
      [root@oldboyedu ~]# file /oldboy/oldboy.txt
      /oldboy/oldboy.txt: ASCII text

b 显示命令绝对路径信息: which

          [root@oldboyedu ~]# which cat
          /usr/bin/cat

          只会在PATH路径中找

c 显示命令路径以及文件: whereis 

        作用: 显示命令路径以及命令相关的帮助手册文件路径

d 快速定位文件路径信息 locate、 updatadb

          locate 文件名称     --- 可以显示文件所在的路径信息
          updatedb             --- 建立文件和目录结构对应关系,便于检索
          PS: 默认centos7没有以上命令,需要安装软件
          yum install -y mlocate
1.首先使用一下
[xuezy@xue~]$ locate ifcfg-eth0
-bash: locate: command not found
2.没有安装,需要安装一下
[root@xue/home/xuezy]# yum install -y locate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                    | 3.6 kB  00:00:00     
epel                                                                                                    | 4.7 kB  00:00:00     
extras                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                 | 2.9 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                             |  96 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                           | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                                           | 6.9 MB  00:00:01     
No package locate available.
Error: Nothing to do

3.镜像中没找到
[root@xue/home/xuezy]# yum --help
Loaded plugins: fastestmirror
Usage: yum [options] COMMAND

provides       Find what package provides the given value
4.找到这条指令属于哪个安装包
[root@xue/home/xuezy]# yum provides locate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
epel/x86_64/filelists_db                                                                                |  12 MB  00:00:02     
extras/7/x86_64/filelists_db                                                                            | 226 kB  00:00:00     
updates/7/x86_64/filelists_db                                                                           | 3.4 MB  00:00:00     
mlocate-0.26-8.el7.x86_64 : An utility for finding files by name
Repo        : base
Matched from:
Filename    : /usr/bin/locate

5.找到后,安装
[root@xue/home/xuezy]# yum install -y  mlocate

验证一下有没有安装好

[root@xue/home/xuezy]# rpm -qa mlocate
mlocate-0.26-8.el7.x86_64

[root@xue/home/xuezy]# rpm -ql  mlocate
/usr/bin/locate

再次使用,发现没数据库文件      

[root@xue/home/xuezy]# locate ifcfg-eth0
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

更新数据库

[root@xue/home/xuezy]# update
bash: update: command not found
[root@xue/home/xuezy]# updatedb
[root@xue/home/xuezy]# ls -l /var/lib/mlocate/mlocate.db 
-rw-r-----. 1 root slocate 2110598 Mar 16 15:19 /var/lib/mlocate/mlocate.db

再次使用

[root@xue/home/xuezy]# locate ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth0

e 快速查找数据信息命令   find

          find命令语法
          find 找寻的路径范围 -type 类型信息 -name "文件名称"      

          [root@oldboyedu etc]# find  /etc -type f -name "ifcfg-eth0"
          /etc/sysconfig/network-scripts/ifcfg-eth0

          问题: 一个文件名称没有记全,如何查看文件路径

          [root@oldboyedu etc]# find  /etc -type f -name "*eth0"
          /etc/sysconfig/network-scripts/ifcfg-eth0
          [root@oldboyedu etc]# find  /etc -type f -name "if*h0"
          /etc/sysconfig/network-scripts/ifcfg-eth0
          
          忽略大小写:
          [root@oldboyedu etc]# find  /etc -type f -iname "if*h0"

基础作业:
01. 总结find命令 
02. 总结文件属性信息 

扩展作业:
01. 如何产生随机字符信息
    利用/dev/urandom文件生成随机数 
    新来10个运维 10个账号 

5. find命令查找信息补充

查看找文件数据信息:
精确查找:

   find 路径信息 -type 文件类型 -name "文件名"

 模糊查找:

    find 路径信息 -type 文件类型 -name "文件名*"
    find 路径信息 -type 文件类型 -name "*文件名"    
    find 路径信息 -type 文件类型 -name "文*件名"

忽略大小写查找:

    find 路径信息 -type 文件类型 -iname "文件名*"

根据数据大小查找数据:

    find /oldboy -type f -size +100   --- 找出大于100K的文件
    find /oldboy -type f -size -100
    find /oldboy -type f -size +1M    --- 找出大于1M的文件
    `b'    for 512-byte blocks (this is the default if no suffix is used)
    `c'    for bytes   (推荐)
    `w'    for two-byte words
    `k'    for Kilobytes (units of 1024 bytes) (推荐)
    `M'    for Megabytes (units of 1048576 bytes) (推荐)
    `G'    for Gigabytes (units of 1073741824 bytes)

根据目录指定层级进行查找数据(进行递归查找)

默认是递归插找的,如在不同目录下,同名的文件就会被找出来

[root@oldboyedu oldboy01]# find /oldboy -maxdepth 1 -type f -name "Oldboy*"
    /oldboy/Oldboy.txt
[root@oldboyedu oldboy01]# find /oldboy -maxdepth 2 -type f -name "Oldboy*"
    /oldboy/oldboy01/Oldboy.txt
    /oldboy/Oldboy.txt
[root@oldboyedu oldboy01]# find /oldboy  -type f -name "Oldboy*" -maxdepth 1
    find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional 
(-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
    
    /oldboy/Oldboy.txt

根据数据的权限进行查找数据   -perm

	     [root@oldboyedu oldboy]# find /oldboy -maxdepth 1 -type f -perm 644
         /oldboy/services
         /oldboy/oldboy.txt
         /oldboy/oldboy.jpg
         /oldboy/oldboy.png
         /oldboy/oldboy.avi
         /oldboy/1.tar

根据数据的时间信息查找  将30天以前的所有数据进行删除

find / -type f -mtime +30 -delete

find / -type f -mtime -10  最近十天的

find / -type f -mtime +10  十天前的

find / -type f -mtime  10  正好十天

6. 显示目录结构信息

[root@xue/etc]# yum install -y tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                    | 3.6 kB  00:00:00     
epel                                                                                                    | 4.7 kB  00:00:00     
extras                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                 | 2.9 kB  00:00:00     
(1/2): epel/x86_64/updateinfo                                                                           | 1.0 MB  00:00:06     
(2/2): epel/x86_64/primary_db                                                                           | 6.9 MB  00:00:07     
Package tree-1.6.0-10.el7.x86_64 already installed and latest version
Nothing to do
[root@xue/home]# tree .
.
├── test
└── xuezy
    ├── test1
    │   ├── oldboy01
    │   ├── oldboy02
    │   ├── oldboy03
    │   ├── oldboy04
    │   ├── oldboy05
    │   ├── oldboy06
    │   ├── oldboy07
    │   ├── oldboy08
    │   ├── oldboy09
    │   ├── oldboy10
    │   └── test1.txt
    ├── test2
    │   └── test2.txt
    ├── test4
    ├── testing
    └── test.txt

    tree   /oldboy            --- 显示指定目录中的所有数据和所有结构信息
    tree   -L 2   /              --- 查看目录结构层级信息
    tree -d   /oldboy        --- 显示目录中所有结构信息      

 7. 系统中如何对文件进行压缩处理

   压缩的命令  tar 
    压缩命令语法:
    tar zcvf /oldboy/oldboy.tar.gz  指定要压缩的数据文件  
    z  压缩的方式 为zip
    c  创建压缩包文件
    v  显示压缩的过程
    f  指定压缩包文件路径信息

    压缩数据方法:
    [root@oldboyedu oldboy]# tar zcvf /oldboy/oldboy.tar.gz /oldboy/services 
    tar: Removing leading `/' from member names
    /oldboy/services

    检查是否压缩成功:

   如何解压数据包
    tar xvf /oldboy/oldboy.tar.gz

   如何解压数据:
	   tar xf /tmp/oldboy.tar.gz -C /oldboy

  确认解压后的文件和源文件是否一致
    1. 看文件的大小和时间信息
    2. 利用vimdiff或者diff比较两个文件内容是否有区别

[root@xue/home/test]# diff ./test01.txt ./test02.txt 
2d1
<  first line 

[root@xue/home/test]# cat test01.txt 
 first line 
 first line 
[root@xue/home/test]# cat test02.txt
 first line 
[root@xue/home/test]# vimdiff test01.txt test02.txt 
2 files to edit


    在不解压情况下查看压缩包里面的数据

    tar tf /oldboy/oldboy.tar.gz

压缩过程信息说明

    [root@oldboyedu oldboy]# tar zcvf oldboy.tar.gz /oldboy/services 
    tar: Removing leading `/' from member names     --- 默认将压缩时绝对路径的根信息移除
    /oldboy/services  --- 需要压缩的文件数据信息
    相对路径进行压缩
    [root@oldboyedu oldboy]# tar zcvf oldboy.tar.gz ./services 
    ./services
    [root@oldboyedu oldboy]# tar tf oldboy.tar.gz 
    ./services

    在压缩过程中排除指定数据不要被压缩
    tar命令排除参数
        --exclude=PATTERN   exclude files, given as a PATTERN  -- 排除指定单个文件数据
         -X, --exclude-from=FILE    exclude patterns listed in FILE    -- 批量排除多个文件
      

相对路径对应相对路径,绝对路径对应绝对路径

    
    多个文件进行排除
    第一个历程: 编写好排除文件

    [root@oldboyedu oldboy]# vim /tmp/exclude.txt 
    /oldboy/oldboy.txt
    /oldboy/oldboy.avi
    /oldboy/oldboy.jpg
    /oldboy/oldboy.png

   第二个历程: 执行命令进行批量排除

    [root@oldboyedu oldboy]# tar zcvf /tmp/oldboy05.tar.gz /oldboy --exclude-from=/tmp/exclude.txt 
    tar: Removing leading `/' from member names
    /oldboy/
    /oldboy/services  
    /oldboy/oldboy01/

    总结:排除指定数据信息需要定义好路径信息
    路径信息全部采用绝对路径

    [root@oldboyedu /]# tar  zcvf /tmp/oldboy03.tar.gz /oldboy --exclude=/oldboy/oldboy.txt
    tar: Removing leading `/' from member names
    /oldboy/
    /oldboy/services
    /oldboy/oldboy01/

8. 文件属性信息详细说明--文件权限

  1) 文件数据权限有什么:
       r read          --- 读权限   4
       w write         --- 写权限   2
       x execute     --- 执行权限 1
       -            --- 没有权限 0
    2) 权限赋予到指定人
       属主信息  文件所有者(监护人)      权限 
       属组信息  文件所有者(亲人)        权限 
       其他用户  隔壁老王                权限       
 
    这个文件权限是644权限:
    属主            属组     其他用户 
    r(4)w(2)-(0)=6  r(4)--=4 r(4)--=4
    
    练习:
    rwx -- 7 
    rw--w---x  621
    rwx-wx---  730
    432        r---wx-w-

9. 文件属性信息详细说明---用户信息(属主 属组)

    1) 系统中用户信息有什么用
       a 利用用户登录系统
       b 利用用户管理数据
       c 利用用户管理进程       
    2) 系统如何识别不同的用户
       利用用户的数值编号进行识别: uid

       [root@oldboyedu ~]# id root
       uid=0(root) gid=0(root) groups=0(root)
       [root@oldboyedu ~]# id oldboy
       uid=1000(oldboy) gid=1000(oldboy) groups=1000(oldboy)

  3) 用户组的概念:
       oldboy oldgirl olddog(一个组)  -sa(system admin)    /sa  --rwx 
       boy gril dog(一个组)           -dev(Development)    /dev --rwx  
       alex old (一个组)              -dba(database admin) /dba --rwx
       
       用户和用户组的关系:
       用户            -- 用户组         1v1
       多个用户     -- 用户组         多v1
       用户         -- 多个用户组     1v多
       系统识别用户组也是根据编号识别: gid
       
       [root@oldboyedu ~]# id root
       uid=0(root) gid=0(root) groups=0(root)
          01          02           03
       01. 显示用户名和uid对应关系
       02. 显示属于的用户组,以及组名和gid对应关系
       03. 用户可能属于哪些组
   
    4) 用户进行分类
       a 超级管理员用户     皇帝   root
         uid信息进行区分: 0      
         从权限上区分:    至高无上         
       b 虚拟用户           傀儡   xxx     nobody
         uid信息进行区分: 1-999  centos7
         从权限上区分:    管理进程  不能登录系统/没有家目录
       c 普通用户           平民   oldboy oldgirl
         uid信息进行区分: 1000+  centos7
         从权限上区分:    管理指定数据 可以管理指定进程 可以登录系统/有家目录

       
       如何保护好皇帝:
       01. 修改远程连接端口-52113     --- 修改/etc/ssh/sshd_config
       02. 禁止root用户远程登录       --- 修改/etc/ssh/sshd_config    

       
       root --- 密码破解
       1. 系统中有什么普通用户?
       2. 普通用户密码?
       3. su - root root用户密码
        
       满足什么条件可以以root身份登录系统:
       01. 服务器地址信息  14.215.177.38
       02. 服务器端口信息  22      该端口 
       03. 登录用户信息    root 
       04. 登录用户的密码  rsdasd  暴力破解 
    

10. 文件属性信息详细说明---inode 

     磁盘操作处理过程: 
     磁盘==========>> 地皮
     raid==========>>将多块地连接起来
     分区=========>>将地皮进行规划
     格式化========>>地皮进行打扫整理
     创建文件系统===>>建房风格            inode和block
     挂载==========>>安装门

     inode: 索引节点
    文件或数据在磁盘中保存位置,用于存储文件属性信息,创建文件系统之后才诞生出来
     在同一分区中,两个文件的inode号码相同,两个文件互为硬链接

查看方法:
[xuezy@xue~]$ df -hi
Filesystem     Inodes IUsed IFree IUse% Mounted on
devtmpfs         245K   401  245K    1% /dev
tmpfs            248K     1  248K    1% /dev/shm
tmpfs            248K   728  247K    1% /run
tmpfs            248K    16  248K    1% /sys/fs/cgroup
/dev/sda3         24M   80K   24M    1% /
/dev/sda1        100K   333  100K    1% /boot
tmpfs            248K     1  248K    1% /run/user/1000
[xuezy@xue~]$ df -i
Filesystem       Inodes IUsed    IFree IUse% Mounted on
devtmpfs         250792   401   250391    1% /dev
tmpfs            253487     1   253486    1% /dev/shm
tmpfs            253487   728   252759    1% /run
tmpfs            253487    16   253471    1% /sys/fs/cgroup
/dev/sda3      25062912 81187 24981725    1% /
/dev/sda1        102400   333   102067    1% /boot
tmpfs            253487     1   253486    1% /run/user/1000


    block: 索引节点指向的位置 (数据块)
    文件或数据的具体内容,创建文件系统之后才诞生出来

     特点说明:
     block用于存放的是数据信息
     block默认大小为4k,可以进行调整
     比较大的文件,会占用多个block
     比较小的文件,剩余空间无法使用, 浪费磁盘空间
     创建一个文件至少占用一个inode和一个block(非空文件)
     磁盘读取数据按block为单位读取的
     读取一个block会消耗一次磁盘I/O(input/output 磁盘读写)

	 查看方法:
[xuezy@xue~]$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs         1003168       0   1003168   0% /dev
tmpfs            1013948       0   1013948   0% /dev/shm
tmpfs            1013948    9780   1004168   1% /run
tmpfs            1013948       0   1013948   0% /sys/fs/cgroup
/dev/sda3       50101352 2494000  47607352   5% /
/dev/sda1         201380  133304     68076  67% /boot
tmpfs             202792       0    202792   0% /run/user/1000
[xuezy@xue~]$ df  -hi
Filesystem     Inodes IUsed IFree IUse% Mounted on
devtmpfs         245K   401  245K    1% /dev
tmpfs            248K     1  248K    1% /dev/shm
tmpfs            248K   728  247K    1% /run
tmpfs            248K    16  248K    1% /sys/fs/cgroup
/dev/sda3         24M   80K   24M    1% /
/dev/sda1        100K   333  100K    1% /boot
tmpfs            248K     1  248K    1% /run/user/1000

 企业面试题:    读取数据的原理
    01. 找到指定数据
    02. 读取数据内容,先获取文件数据inode信息
    03. 根据inode信息在磁盘上找到对应block
    04. 根据block信息获取文件真正内容    

企业中面试: 你都做过哪些系统优化操作?
     问题: block默认大小可以调整 是大点好 小点好
     设置大点: 10k 视频公司 图片公司
     文件大的时候: 20K 4k -- 2个block 2s 4s   --- 节省IO消耗

     设置小点: 2k 互联网公司
     小文件的时候: 1k 2k  -- 1个block         --- 提高磁盘利用率 

       企业面试题: 如何将数据真正的删除?
       01. 确保文件的硬链接数为0   i_link
       02. 确保文件没有被进程占用  i_count

     补充:
     01. 文件属性信息存储在inode中
     02. 文件的名称信息存储在上一级目录的block中

# vim  ./

" Netrw Directory Listing                                        (netrw v149)
"   /home/xuezy
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:exec
" ============================================================================
../
./
test1/
test2/
test4/
testing/
.bash
.bash_history

...

11. 文件属性信息详细说明--时间信息(改变时间)

   文件数据时间类型:
    01. 访问时间  Access time == atime 
    02. 修改时间  Modify time == mtime 
    03. 改变时间  Change time == ctime  --- 数据属性信息发生改变了

[xuezy@xue~]$ touch new_file.txt
[xuezy@xue~]$ stat new_file.txt 
  File: ‘new_file.txt’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d	Inode: 67716701    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   xuezy)   Gid: ( 1000/   xuezy)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2021-03-21 11:10:51.986453175 +0800
Modify: 2021-03-21 11:10:51.986453175 +0800
Change: 2021-03-21 11:10:51.986453175 +0800
 Birth: -
[xuezy@xue~]$ echo xuezy >> new_file.txt 
[xuezy@xue~]$ stat new_file.txt 
  File: ‘new_file.txt’
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 67716701    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   xuezy)   Gid: ( 1000/   xuezy)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2021-03-21 11:17:28.858445453 +0800
Modify: 2021-03-21 11:49:36.134061962 +0800
Change: 2021-03-21 11:49:36.134061962 +0800
 Birth: -
[xuezy@xue~]$ chmod 666 new_file.txt 
[xuezy@xue~]$ stat new_file.txt 
  File: ‘new_file.txt’
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 67716701    Links: 1
Access: (0666/-rw-rw-rw-)  Uid: ( 1000/   xuezy)   Gid: ( 1000/   xuezy)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2021-03-21 11:17:28.858445453 +0800
Modify: 2021-03-21 11:49:36.134061962 +0800
Change: 2021-03-21 11:50:24.460737760 +0800
 Birth: -

12.文件属性信息-硬链接数

硬链接:
    概念说明:  在一个分区中,inode号码相同的文件,互为硬链接
    诞生过程:

 ln    链接的源文件    硬链接的文件信息
[root@xue/home/xuezy]# ln /home/xuezy/test.txt /home/xuezy/test_hard_link.txt

[root@xue/home/xuezy]# ll
-rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test_hard_link.txt
-rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test.txt

[root@xue/home/xuezy]# ll -i
 67716698 -rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test.txt
 67716698 -rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test_hard_link.txt

软链接:
    概念说明: 文件的快捷方式
    诞生过程:    ln -s 链接的源文件  软链接的文件信息

[root@xue/home/xuezy]# ln -s /home/xuezy/test.txt  /home/xuezy/test_soft_link.txt

[root@xue/home/xuezy]# ll
-rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test.txt
-rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test_hard_link.txt
lrwxrwxrwx.  1 root  root    20 Mar 28 16:41 test_soft_link.txt -> /home/xuezy/test.txt

[root@xue/home/xuezy]# ll -i
 67716698 -rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test.txt
 67716698 -rw-rw-r--.  2 xuezy xuezy   21 Mar 21 23:59 test_hard_link.txt
 67199876 lrwxrwxrwx.  1 root  root    20 Mar 28 16:41 test_soft_link.txt -> /home/xuezy/test.txt

删除之后查看:

[root@xue/home/xuezy]# rm -f test.txt 
[root@xue/home/xuezy]# ll -i

[root@xue/home/xuezy]# cat ./test_hard_link.txt 
hello 
this
is
a
txt
[root@xue/home/xuezy]# cat ./test_soft_link.txt 
cat: ./test_soft_link.txt: No such file or directory

重新输入之后查看内容

[root@xue/home/xuezy]# echo hello world > ./test.txt
[root@xue/home/xuezy]# cat ./test_hard_link.txt 
hello 
this
is
a
txt
[root@xue/home/xuezy]# cat ./test_soft_link.txt 
hello world

软硬链接的区别:
    01. 链接后的文件大小不一致
    02. 链接后的文件类型和权限不一致(颜色不一致)
    03. 链接后的文件和源文件 inode信息有出入
        硬链接inode号码一致
        软链接inode号码不一样
    04. 源文件被删除之后
        硬链接文件依然有效
        软链接文件失效
    05. 软硬链接创建过程不一致

查找一个文件的硬链接数目

[root@xue/home/xuezy]# find / -type f -inum 67199875
/home/xuezy/test.txt
/home/xuezy/test_hard_01.txt
/home/xuezy/test_hard_02.txt

给文件创建软链接、硬链接

系统中的目录不能创建硬链接:

[root@xue/home/test]# ln -s  /etc/  ./test_soft_etc
[root@xue/home/test]# ll
total 0
lrwxrwxrwx. 1 root root 5 Mar 29 15:09 test_soft_etc -> /etc/

[root@xue/home/test]# ln   /etc/  ./test_hard_etc
ln: ‘/etc/’: hard link not allowed for directory

/proc  /sys 虽然inode都是1,它们是存到内存中的。与磁盘中的不相同。

        1 dr-xr-xr-x.  13 root root    0 Mar 29 11:32 sys

        1 dr-xr-xr-x. 139 root root    0 Mar 29 11:32 proc

原因就是允许目录的硬链接可能会打破文件系统目录的有向无环图结构,可能创建目录循环,这可能会导致fsck以及其他一些遍历文件树的软件出错。这可能会出现一种你的父目录是你的子目录这种及其奇怪的事情出现,其实就是硬链接以后的目录可能是你的父目录,这导致很多遍历系统的命令如果不跟踪inode的话就没办法用了,因为可能会导致无限循环,比如du,ls -r, pwd等。
————————————————
版权声明:本文为CSDN博主「一个划水的人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43705457/article/details/106377853

扩展:

[root@xue/home/test]# mkdir oldboy
[root@xue/home/test]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 29 15:42 oldboy

新的文件夹硬链接数目是:2

解析:如下,有本目录和. 所以有两个硬链接

[root@xue/home/test]# ll -id ./oldboy
101864266 drwxr-xr-x. 2 root root 6 Mar 29 15:42 ./oldboy


[root@xue/home/test]# ll -ia ./oldboy
total 0
101864266 drwxr-xr-x. 2 root root   6 Mar 29 15:42 .
   239009 drwx------. 3 test test 118 Mar 29 15:42 ..
查找子目录的数量

[root@xue/home/test]# ll /etc/ |grep -c "^d"
85
[root@xue/home/test]# ll /etc/ |grep "^d" |wc -l
85
[root@xue/home/test]# ll -d /etc
drwxr-xr-x. 87 root root 8192 Mar 29 13:20 /etc

87 - 本身1个 -目录中的 . = 85个

13.时间相关的命令

时间相关的命令 date

星期一 Monday Mon.,星期二 Tuesday Tues.,星期三 Wednesday Wed.,星期四 Thursday Thur./Thurs.,星期五 Friday Fri.,星期六 Saturday Sat.,星期日 Sunday Sun.。

月份的英文及其缩写分别为:一月 Jan. January,二月 Feb. February,三月 Mar. March,四月 Apr. April,五月 May. May,六月 Jun. June,七月 Jul. July,八月 Aug. August,九月 Sept. September,十月 Oct. October,十一月 Nov. November,十二月 Dec. December。

    01. 查看时间信息:  

[root@xue/home/test]# date
Mon Mar 29 16:04:08 CST 2021

   02. 调整时间显示格式

[root@xue/home/test]# date "+%F-%T"
2021-03-29-16:08:03
[root@xue/home/test]# date +%F-%T
2021-03-29-16:08:10
[root@xue/home/test]# date "+%F %T"
2021-03-29 16:08:16
[root@xue/home/test]# date "+%y-%m-%d  %H:%M%S"
21-03-29  16:0947

用处
   日志备份时间戳

[root@xue/home/xuezy]# cp ./test.log test.log-`date +%F_%T`
[root@xue/home/xuezy]# cp ./test.log test.log-$(date +%F_%T)

-rw-r--r--.  1 root  root     0 Mar 29 16:15 test.log-2021-03-29_16:15:47
-rw-r--r--.  1 root  root     0 Mar 29 16:16 test.log-2021-03-29_16:16:03

   显示历史时间信息:

    # date +%F -d "-2day"
    # date +%F -d "1 day ago"

显示未来时间信息:

    # date  -d "+2day"
    # date  -d "2day"
系统自动的备份数据 00:00  4月16日 -- 4月17日 00:00   16号的数据备份的时候是17号,所以要减一天

    cp /oldboy/oldboy.log /log/oldboy.log.$(date +%F)
调整: 

    cp /oldboy/oldboy.log /log/oldboy.log.$(date +%F -d "-1day")

03. 如何实际修改系统时间

    [root@oldboyedu oldboy]# date -s "2020-04-17"
    Fri Apr 17 00:00:00 CST 2020
    [root@oldboyedu oldboy]# date
    Fri Apr 17 00:00:17 CST 2020
    [root@oldboyedu oldboy]# date -s "2020/04/17 14:00"
    Fri Apr 17 14:00:00 CST 2020

    04. 时间同步方法

    yum install -y ntpdate
    [root@oldboyedu oldboy]# ntpdate ntp1.aliyun.com
    16 Apr 12:22:33 ntpdate[25718]: step time server 120.25.115.20 offset 268748517.381960 sec
    [root@oldboyedu oldboy]# date
    Tue Apr 16 12:22:48 CST 2019

 

作业
01. 如何将一个目录中以.txt结尾的文件,统一进行压缩处理
    /oldboy/ 
    oldboy01.txt 1 
    oldboy02.txt 
    oldboy03.txt 
    oldboy04.jpg 
    oldboy05.png

    思路:
    01. 将以.txt结尾的文件找出来
    02. 利用tar命令再进行打包

Ctrl+R 可以搜索之前的命令,输入touch ,找到后,输入方向键即可。

[root@xue/home/xuezy]# find ./ -type f -name "oldboy*.txt"
./oldboy01.txt
......
./oldboy10.txt
[root@xue/home/xuezy]# find ./ -maxdepth 1 -type f -name "oldboy*.txt" |xargs tar zcvf ./oldboy.tar.gz
./oldboy01.txt
.......
./oldboy10.txt
[root@xue/home/xuezy]# tar tf ./oldboy.tar.gz 
./oldboy01.txt
.......
./oldboy10.txt
这个也可以
[root@xue/home/xuezy]# tar zcvf ./oldboy01.tar.gz `find ./ -maxdepth 1 -type f -name "*.txt"`


02. 如何找出/oldboy目录中.txt结尾的文件,将找出的文件进行统一删除

[xuezy@xue~]$ find /oldboy/ -type f -name "*.txt" -delete 
[xuezy@xue~]$ find /oldboy/ -type f -name "*.txt" -exec rm -rf {} \;
[xuezy@xue~]$ find /oldboy/ -type f -name "*.txt" | xargs rm -f

。。。。xargs 将多行信息,一行输出

[xuezy@xue~]$ cat test.txt 
hello 
this
is
a
txt

[xuezy@xue~]$ xargs < ./test.txt
hello this is a txt

[xuezy@xue~]$ xargs -n2 < ./test.txt
hello this
is a
txt

[xuezy@xue~]$ xargs -n3 < ./test.txt
hello this is
a txt

03. 如何找出/oldboy目录中.txt结尾的文件,将找出的文件进行批量复制/移动到/tmp目录中    

[xuezy@xue~]$ find /oldboy/ -type f -name "*.txt" -exec cp {} /tmp  \;

04. centos6里面如何用uid区分不同的用户类型
05. 总结linux系统中常用的压缩和解压命令 tar zip rar

作业:
01. 如何修改磁盘中block大小 如何进行查看默认的block大小
02. 总结数据存储与读取原理     
03. 磁盘满无法存储数据的原因    
04. linux系统中读取数据的原理(数据无法读取-权限阻止)  权限章节
    /oldboy/oldboy01/oldboy.txt
05. 总结英文月份信息 星期信息
06. 如何将文件彻底删除

       企业面试题: 如何将数据真正的删除?
       01. 确保文件的硬链接数为0   i_link
       02. 确保文件没有被进程占用  i_count

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值