00. 目录
01. 命令概述
chattr命令用来改变文件属性。这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下几种模式:
参数 | 作用 |
---|---|
i | 无法对文件进行修改;若对目录设置了该参数,则仅能修改其中的子文件内容而不能新建或删除文件 |
a | 仅允许补充(追加)内容,无法覆盖/删除内容(Append Only) |
S | 文件内容在变更后立即同步到硬盘(sync) |
s | 彻底从硬盘中删除,不可恢复(用0填充原文件所在硬盘区域) |
A | 不再修改这个文件或目录的最后访问时间(atime) |
b | 不再修改文件或目录的存取时间 |
D | 检查压缩文件中的错误 |
d | 使用dump命令备份时忽略本文件/目录 |
c | 默认将文件或目录进行压缩 |
u | 当删除该文件后依然保留其在硬盘中的数据,方便日后恢复 |
t | 让文件系统支持尾部合并(tail-merging) |
x | 可以直接访问压缩文件中的内容 |
如果想要把某个隐藏功能添加到文件上,则需要在命令后面追加“+参数”,如果想要把某个隐藏功能移出文件,则需要追加“-参数”。
02. 命令格式
用法:chattr [参数] 文件
03. 常用选项
-R:递归处理,将指令目录下的所有文件及子目录一并处理;
-v<版本编号>:设置文件或目录版本;
-V:显示指令执行过程;
+<属性>:开启文件或目录的该项属性;
-<属性>:关闭文件或目录的该项属性;
=<属性>:指定文件或目录的该项属性。
04. 参考示例
4.1 设置无法对文件进行修改
[root@itcast test]# chattr +i file
[root@itcast test]# echo "神马程序员" >> file
-bash: file: 权限不够
[root@itcast test]#
4.2 撤销i属性
[root@itcast test]# chattr +i file
[root@itcast test]# echo "C++" >> file
-bash: file: 权限不够
[root@itcast test]# chattr -i file
[root@itcast test]# echo "C++" >> file
[root@itcast test]#
4.3 允许补充(追加)内容,无法覆盖/删除内容
[root@itcast test]# chattr +a file
[root@itcast test]# rm -f file
rm: 无法删除"file": 不允许的操作
[root@itcast test]# echo "hello itcast" >> file
[root@itcast test]# cat file
hello itcast
[root@itcast test]#
4.4 撤销a属性
[root@itcast test]# chattr -a file
[root@itcast test]# echo > file
[root@itcast test]#
4.5 显示指令执行过程
[root@itcast test]# chattr -V +i file
chattr 1.42.9 (28-Dec-2013)
file的标志被设为 ----i-----------
[root@itcast test]#
4.6 设置文件或目录版本
[root@itcast test]# chattr -v 1 -V -i file
chattr 1.42.9 (28-Dec-2013)
file的标志被设为 ----------------
Version of file set as 1
chattr: 对设备不适当的 ioctl 操作 while setting version on file
[root@itcast test]#
4.7 删除a属性,增加c属性
[root@itcast test]# chattr -V -v 2 -a +c file
chattr 1.42.9 (28-Dec-2013)
file的标志被设为 --------c-------
chattr: 不支持的操作 while setting flags on file
[root@itcast test]#