Linux常用命令-chattr

命令

chattr

描述

change file attributes on a Linux file system
更改文件属性

用法

1
chattr [ -RVf ] [ -v version ] [ mode ] files...

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Options:
-R 递归更改目录属性
-V 显示详情
-f 不显示错误输出
-v version 设置文件的版本号
mode: +-=[aAcCdDeijsStTu]

mode
a: append only,追加
A: no atime updates,不更新atime
c: compressed,压缩
C: no copy on write,写时不拷贝
d: no dump,不转储
D: synchronous directory updates,同步目录更新
i: immutable,不可变
j: data journalling,数据日志
s: secure deletion,安全删除
S: synchronous updates,同步更新
T: top of directory hierarchy,目录层次结构的顶部
u: undeletable,不可删除
以下属性为只读
E: compression error,压缩错误
h: huge file,大文件
I: indexed directory,索引目录
N: inline data,内联数据
X: compression raw access,压缩原始访问
Z: compressed dirty file,压缩脏文件

并非所有文件系统都支持以上属性,可通过man xfs,man ext4等命令查看

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$ echo aaa > file
$ lsattr file
---------------- file
# 添加i属性,即锁定文件不可修改和删除
$ chattr +i file
$ lsattr file
----i----------- file
$ echo bbb > file
-bash: file: Permission denied
$ mv file file1
mv: cannot move ‘file’ to ‘file1’: Operation not permitted
$ rm file
rm: remove regular file ‘file’? y
rm: cannot remove ‘file’: Operation not permitted
# 删除i属性,添加a属性,即文件只能追加内容
$ chattr -i file
$ chattr +a file
$ lsattr file
-----a---------- file
$ echo bbb >> file
$ mv file file1
mv: cannot move ‘file’ to ‘file1’: Operation not permitted
# xfs文件系统不支持u属性
$ chattr +u file
chattr: Operation not supported while setting flags on file

$ mkdir dir
$ lsattr dir
# 给目录增加a属性,则可以在目录中新建和修改文件,但不能删除和重命名
$ chattr +a dir
$ echo aaa > dir/a
$ mkdir dir/dir1
$ ll dir
-rw-r--r-- 1 root root 4 Feb 24 19:50 a
drwxr-xr-x 2 root root 6 Feb 24 19:50 dir1/
$ lsattr dir/a
---------------- dir/a
$ echo bbb >> dir/a
$ rm dir/a
rm: remove regular file ‘dir/a’? y
rm: cannot remove ‘dir/a’: Operation not permitted
$ mv dir/a dir/b
mv: cannot move ‘dir/a’ to ‘dir/b’: Operation not permitted