软硬链接、文件删除原理、linux中的三种时间、chkconfig优化

简介: 第1章 软硬链接 1.1 硬链接 1.1.1 含义 多个文件拥有相同的inode号码 硬链接即文件的多个入口 1.1.2 作用 防止你误删除文件 1.1.3 如何创建硬链接 ln 命令,前面是源文件 后面是创建的链接文件 [root@znix oldboy]# ln oldboyedu.txt oldboyedu.txt-hard        查看两文件的inode号相同。

第1章 软硬链接

1.1 硬链接

1.1.1 含义

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

硬链接即文件的多个入口

1.1.2 作用

防止你误删除文件

1.1.3 如何创建硬链接

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

[root@znix oldboy]# ln oldboyedu.txt oldboyedu.txt-hard

       查看两文件的inode号相同。

[root@znix oldboy]# ls -lhi oldboyedu.txt oldboyedu.txt-hard

151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 oldboyedu.txt

151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 oldboyedu.txt-hard

1.2 软连接

1.2.1 含义

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

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

1.2.2 创建软连接

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

[root@znix oldboy]# ln -s oldboyedu.txt oldboyedu.txt-soft

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

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

[root@znix oldboy]# ll -i oldboyedu*

151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 oldboyedu.txt

132910 -rw-r--r-- 1 root root 607 Aug 30 09:14 oldboyedu.txt.bak

151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 oldboyedu.txt-hard

132951 lrwxrwxrwx 1 root root  13 Aug 30 09:22 oldboyedu.txt-soft -> oldboyedu.txt

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@znix ~]# lsof /var/log/secure

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME

rsyslogd 1262 root    4w   REG    8,3     1927 270856 /var/log/secure

[root@znix ~]# lsof |grep message

rsyslogd  1262      root    1w      REG                8,3     1044     270855 /var/log/messages

2.3 重启对应的软件/服务

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

[root@znix ~]# /etc/init.d/rsyslog

Usage: /etc/init.d/rsyslog {start|stop|restart|condrestart|try-restart|reload|force-reload|status}

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

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

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

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

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

2.5 故障案例

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

2.5.1 环境

/var/log/message中放入大量数据

seq 100000000 >>/var/log/messages

2.5.2 查看此时此刻磁盘使用情况

[root@znix apache]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  3.9G  3.7G  59% /

tmpfs           238M     0  238M   0% /dev/shm

/dev/sda1       190M   40M  141M  22% /boot

2.5.3 删除文件

[root@znix apache]# \rm -f /var/log/messages

2.5.4 检查空间没有被释放

[root@znix apache]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  3.9G  3.7G  59% /

tmpfs           238M     0  238M   0% /dev/shm

/dev/sda1       190M   40M  141M  22% /boot

2.5.5 查看被删除(硬链接数为0)但是还被进程调用的文件

[root@znix apache]# lsof |grep delete

rsyslogd  1168      root    1w      REG                8,3 889335632     259962 /var/log/messages (deleted)

2.5.6 重启对应的服务

[root@znix apache]# /etc/init.d/rsyslog restart

Shutting down system logger:                               [  OK  ]

Starting system logger:                                    [  OK  ]

       查看磁盘空间

[root@znix apache]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  6.9G  18% /

tmpfs           238M     0  238M   0% /dev/shm

/dev/sda1       190M   40M  141M  22% /boot

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

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

[root@znix ~]# ls -lhi  test.txt

260141 -rw-r--r--. 2 root root 265 Aug 29 19:16 test.txt

[root@znix ~]# find /* -type f -inum 260141

/root/test.txt

/root/test.txt-hard

第4章 三种时间戳

4.1 含义

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

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

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

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

[root@znix ~]# stat oldboy.txt

  File: `oldboy.txt'

  Size: 237         Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d  Inode: 16976       Links: 2

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-08-28 11:45:26.734849384 +0800

Modify: 2017-08-28 11:45:26.734849384 +0800

Change: 2017-08-30 11:30:27.783364422 +0800

4.3 修改mtime&change

[root@znix ~]# echo "123">>oldboy.txt

[root@znix ~]# stat oldboy.txt

  File: `oldboy.txt'

  Size: 241         Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d  Inode: 16976       Links: 2

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-08-28 11:45:26.734849384 +0800

Modify: 2017-08-30 11:40:57.384368932 +0800

Change: 2017-08-30 11:40:57.384368932 +0800

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

[root@znix ~]# ln oldboy.txt  oldboy.txt-hard

[root@znix ~]# stat oldboy.txt

  File: `oldboy.txt'

  Size: 241         Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d  Inode: 16976       Links: 3

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-08-28 11:45:26.734849384 +0800

Modify: 2017-08-30 11:40:57.384368932 +0800

Change: 2017-08-30 11:42:32.981364780 +0800

4.5 atime修改

[root@znix ~]# tail -1 oldboy.txt

123

[root@znix ~]# stat oldboy.txt

  File: `oldboy.txt'

  Size: 241         Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d  Inode: 16976       Links: 3

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-08-30 11:43:15.288357930 +0800

Modify: 2017-08-30 11:40:57.384368932 +0800

Change: 2017-08-30 11:42:32.981364780 +0800

 

第5章 chkconfig命令相关

chkconfig命令实际上控制的是/etc/rc3.d/3运行模式下)下的软连接。通过不通的软连接,实现不通的控制。实际受/etc/init.d/下脚本的关系。

5.1 执行chkconfig iptables on & chkconfig iptables off之后发生了什么?

执行chkconfig iptables on/etc/rc3.d/下的/etc/init.d/iptables改为S08iptables

[root@znix rc3.d]# chkconfig iptables on

[root@znix rc3.d]# chkconfig |grep ipt

iptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@znix rc3.d]# ls -l /etc/rc3.d/ |grep ipt

lrwxrwxrwx  1 root root 18 Aug 30 12:03 S08iptables -> ../init.d/iptables

       执行chkconfig iptables off /etc/rc3.d/下的/etc/init.d/iptables软连接改为K92iptables

[root@znix rc3.d]# chkconfig iptables off

[root@znix rc3.d]# chkconfig |grep ipt

iptables       0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@znix rc3.d]# ls -l /etc/rc3.d/ |grep ipt

lrwxrwxrwx  1 root root 18 Aug 30 12:04 K92iptables -> ../init.d/iptables

iptables 开机自启动    软连接   S开头

iptables 开机不自启动 软连接   K开头

5.2 让一个软件开机自启动

1)把脚本放入/etc/rc.local

2)通过chkconfig 管理命令或脚本,让他开机自启动

5.3 如何让一个服务或命令通过chkconfig管理

5.3.1 脚本必须放在/etc/init.d/目录下面

[root@znix rc3.d]# echo "hostname" > /etc/init.d/oldboyd

5.3.2 必须写出chkconfig格式

写出chkconfig 才能被chkconfig管理

hkconfig:         2345       99       99

默认在哪几个运行级别启动 开机顺序 关机顺序

自己创建的开机顺序一般写99 99为最后一个

[root@znix rc3.d]# cat /etc/init.d/oldboyd

#!/bin/sh

#

#

# chkconfig: 2345 99 99

#

# description: print hostname

hostname

5.3.3 给这个脚本添加上执行的权限

使用chmod命令修改文件权限

需要有可执行的权限,不然无法执行

[root@znix rc3.d]# chmod +x /etc/init.d/oldboyd

[root@znix rc3.d]# ll /etc/init.d/oldboyd

-rwxr-xr-x 1 root root 9 Aug 30 12:22 /etc/init.d/oldboyd

5.3.4 添加脚本到chkconfig管理

[root@znix rc3.d]# chkconfig --add oldboyd

5.3.5 查看状态

改为开机自启动

[root@znix rc3.d]# chkconfig |grep old

oldboyd        0:off   1:off   2:on    3:on    4:on    5:on    6:off

5.3.6 查看/etc/rc3.d/目录生成相对应的软连接

[root@znix rc3.d]# ls -l /etc/rc3.d/ |grep old

lrwxrwxrwx  1 root root 17 Aug 30 12:38 S99oldboyd -> ../init.d/oldboyd

相关文章
|
29天前
|
Linux 数据处理 C++
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(一)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
69 0
|
存储 Linux 索引
【Linux】—— 详解软硬链接
【Linux】—— 详解软硬链接
|
1月前
|
Linux
linux链接工具有哪些
linux链接工具有哪些
|
1月前
|
Shell Linux 程序员
【Linux】浅谈shell命令以及运行原理
【Linux】浅谈shell命令以及运行原理
|
1月前
|
Linux 网络安全 数据安全/隐私保护
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
143 0
|
1月前
|
存储 弹性计算 人工智能
|
27天前
|
Shell Linux API
【Shell 命令集合 磁盘维护 】Linux 查找指定目录下的所有符号链接文件 symlinks 命令使用教程
【Shell 命令集合 磁盘维护 】Linux 查找指定目录下的所有符号链接文件 symlinks 命令使用教程
31 1
|
29天前
|
存储 Linux API
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(三)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
30 1
|
29天前
|
消息中间件 Linux 数据处理
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用(二)
Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
30 1
|
1月前
|
安全 Linux 网络安全
如何在 VM 虚拟机中安装 Red Hat Enterprise Linux 9.3 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Red Hat Enterprise Linux 9.3 操作系统保姆级教程(附链接)
88 0