linux服务二——软硬链接、文件删除原理、linux中的三种时间

第1章 软硬链接

1.1 硬链接

1.1.1 含义

多个文件拥有相同的inode号码

硬链接即文件的多个入口

1.1.2 作用

防止你误删除文件

1.1.3 如何创建硬链接

ln 命令,前面是源文件 后面是创建的链接文件

[root@iso-all tmp]# ln test.sh a-test.sh 

  查看两文件的inode号相同

[root@iso-all tmp]# ls -lih
总用量 8.0K
16777289 -rw-r--r--. 2 root root 122 9月  19 19:59 a-test.sh
16777289 -rw-r--r--. 2 root root 122 9月  19 19:59 test.sh

1.2 软连接

1.2.1 含义

为了快捷,省事,方便使用

软连接中存放的是源文件的位置

1.2.2 创建软连接

使用ln -s 命令创建软连接

[root@iso-all tmp]# ln -s test.sh b-test.sh

  查看软硬链接的inode号不相同

      但是同时指向的是同一文件

[root@iso-all tmp]# ls -lih
总用量 8.0K
16777289 -rw-r--r--. 2 root root 122 9月  19 19:59 a-test.sh
17958387 lrwxrwxrwx. 1 root root   7 9月  19 21:07 b-test.sh -> test.sh
16777289 -rw-r--r--. 2 root root 122 9月  19 19:59 test.sh
[root@iso-all tmp]# 

1.3 软连接与硬链接的区

1.3.1 含义

       软链接:

              软连接相当于快捷方式

              里面存放的是源文件的位置

       硬链接:

              在同一个分区中,多个文件拥有相同的inode

1.3.2 创建方式不同

              ln 创建硬链接

              ln -s 软连接

1.3.3 不同的特点

1)软连接可以随意创建

2)不能对目录创建硬链接

3)对文件创建硬链接可以防止文件被误删除

1.3.4 如何删除

1)删除文件的硬链接,文件可以继续使用

    2)只有把这个文件的所有硬链接都删除才可

3)只删除源文件软连接无法使用

    4)只删除软连接对文件没有影响

第2章 文件删除原理

2.1 彻底删除一个文件

1.硬链接数为0 与这个文件有关的所有硬链接都被删除。

a)       使用rm目录进行删除

2.     进程调用数为0,没有人在使用这个文件才能释放磁盘空间。

a)       使用lsof 查看谁在使用这文件

b)      重启对应的软件/服务就能释放磁盘

2.2 查看某个文件是否总有人在用

使用lsof命令可以列出所有正在使用的文件和相应的进程

[root@iso-all ~]# lsof /var/log/secure
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
rsyslogd 7038 root    8w   REG  253,0     2793 33671230 /var/log/secure
[root@iso-all ~]# lsof |grep message
rsyslogd  7038         root    6w      REG              253,0     19542   33671229 /var/log/messages
in:imjour 7038 7042    root    6w      REG              253,0     19542   33671229 /var/log/messages
rs:main   7038 7043    root    6w      REG              253,0     19542   33671229 /var/log/messages
[root@iso-all ~]# 

2.3 重启对应的软件/服务

找到软件对应的管理地址,让软件重启,释放空间。

[root@iso-all ~]# systemctl restart rsyslog

2.4 磁盘空间满了三种情况)

       inode满了……查找出系统目录比较大(1M)

       block满了……使用du -sh /* 一层一层找,把较大的文件删除

       硬链接数为0,进程调用数不为0

使用  lsof |grep delete 查看占用的文件

2.5 故障案例

没有被彻底删除-硬链接数为0,进程调用数不为零

[root@iso-all ~]# seq 100000000 >>/var/log/messages     #向 /var/log/message中放入大量数据
[root@iso-all ~]# df -TH                                #查看此时此刻磁盘使用情况
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        21G  3.5G   17G   18% /
devtmpfs                devtmpfs  498M     0  498M    0% /dev
tmpfs                   tmpfs     510M     0  510M    0% /dev/shm
tmpfs                   tmpfs     510M  8.0M  502M    2% /run
tmpfs                   tmpfs     510M     0  510M    0% /sys/fs/cgroup
/dev/sda1               xfs       207M  116M   91M   57% /boot
tmpfs                   tmpfs     102M     0  102M    0% /run/user/0
[root@iso-all ~]# 
m -f /var/log/messages            #删除文件
[root@iso-all ~]# df -TH                              #检查空间没有被释放
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        21G  3.5G   17G   18% /
devtmpfs                devtmpfs  498M     0  498M    0% /dev
tmpfs                   tmpfs     510M     0  510M    0% /dev/shm
tmpfs                   tmpfs     510M  8.0M  502M    2% /run
tmpfs                   tmpfs     510M     0  510M    0% /sys/fs/cgroup
/dev/sda1               xfs       207M  116M   91M   57% /boot
tmpfs                   tmpfs     102M     0  102M    0% /run/user/0
[root@iso-all ~]# lsof |grep delete                       #查看被删除(硬链接数为0)但是还被进程调用的文件
tuned     7035         root    8u      REG              253,0      4096   16777294 /tmp/#16777294 (deleted)
gmain     7035 7281    root    8u      REG              253,0      4096   16777294 /tmp/#16777294 (deleted)
tuned     7035 7282    root    8u      REG              253,0      4096   16777294 /tmp/#16777294 (deleted)
tuned     7035 7283    root    8u      REG              253,0      4096   16777294 /tmp/#16777294 (deleted)
tuned     7035 7297    root    8u      REG              253,0      4096   16777294 /tmp/#16777294 (deleted)
rsyslogd  8039         root    6w      REG              253,0 888909002   33671229 /var/log/messages (deleted)
in:imjour 8039 8041    root    6w      REG              253,0 888909002   33671229 /var/log/messages (deleted)
rs:main   8039 8042    root    6w      REG              253,0 888909002   33671229 /var/log/messages (deleted)
[root@iso-all ~]# systemctl restart rsyslog          #重启对应的服务
[root@iso-all ~]# df -TH                             #查看磁盘空间
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        21G  2.6G   18G   13% /
devtmpfs                devtmpfs  498M     0  498M    0% /dev
tmpfs                   tmpfs     510M     0  510M    0% /dev/shm
tmpfs                   tmpfs     510M  8.0M  502M    2% /run
tmpfs                   tmpfs     510M     0  510M    0% /sys/fs/cgroup
/dev/sda1               xfs       207M  116M   91M   57% /boot
tmpfs                   tmpfs     102M     0  102M    0% /run/user/0

第3章 找出某个文件的其他的硬链接 

使用find命令 -inum参数找inode号码,找到相同的inode 互为硬链接。

[root@iso-all tmp]# ls -ihl 
总用量 8.0K
16777289 -rw-r--r--. 2 root root 122 9月  19 19:59 a-test.sh
17958387 lrwxrwxrwx. 1 root root   7 9月  19 21:07 b-test.sh -> test.sh
16777289 -rw-r--r--. 2 root root 122 9月  19 19:59 test.sh
[root@iso-all tmp]#  find /* -type f -inum 16777289
/tmp/test.sh
/tmp/a-test.sh
[root@iso-all tmp]# 

第4章 三种时间戳

4.1 含义

Modify   mtime修改时间 (最常用) 文件的内容 增加 删除 修改改变

Change   ctime属性变更时间        文件属性发生改变时更改

Access   atime访问时间            查看文件的时间 (只有文件内容有修改时才会改变)

4.2 使用stat命令查看文件的信息

[root@iso-all tmp]# stat test.sh 
  文件:"test.sh"
  大小:122           块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:16777289    硬链接:2
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-09-19 19:59:58.072829155 +0800
最近更改:2020-09-19 19:59:58.073829155 +0800
最近改动:2020-09-19 20:00:31.260828964 +0800
创建时间:-

4.3 修改mtime&change

[root@iso-all tmp]#  echo "123">>test.sh 
[root@iso-all tmp]# stat test.sh 
  文件:"test.sh"
  大小:126           块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:16777289    硬链接:2
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-09-19 19:59:58.072829155 +0800
最近更改:2020-09-19 21:30:31.078797867 +0800
最近改动:2020-09-19 21:30:31.078797867 +0800
创建时间:-

4.4 修改ctime (属性变更时间)

[root@iso-all tmp]# ln test.sh c-test.sh
[root@iso-all tmp]# stat test.sh 
  文件:"test.sh"
  大小:126           块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:16777289    硬链接:3
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-09-19 19:59:58.072829155 +0800
最近更改:2020-09-19 21:30:31.078797867 +0800
最近改动:2020-09-19 21:31:34.102797504 +0800
创建时间:-
[root@iso-all tmp]# 

4.5 atime修改

[root@iso-all tmp]#  tail -1 test.sh 
123
[root@iso-all tmp]# stat test.sh 
  文件:"test.sh"
  大小:126           块:8          IO 块:4096   普通文件
设备:fd00h/64768d    Inode:16777289    硬链接:3
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-09-19 21:32:34.698797155 +0800
最近更改:2020-09-19 21:30:31.078797867 +0800
最近改动:2020-09-19 21:31:34.102797504 +0800
创建时间:-

 

原文地址:https://www.cnblogs.com/huihuangyan/p/13698357.html