名称

        chown - 改变文件的属主或属组

用法

        chown [OPTION]... [OWNER][:[GROUP]] FILE...
        chown [OPTION]... --reference=RFILE FILE...

描述

chown改变每个文件的属主和/或属组。如果只给定了一个属主(一个用户名或用户ID),每个文件的属主会被配置成这个用户,但是属组不变。如果属主后边加上冒号和组名(或组ID),属组也会被修改。如果冒号后边没有指定组名,修改的属组为用户的登录组。如果只有冒号和组名,但没有用户名,chown的功能与chgrp一致。如果只有一个冒号,或者为空,属主和属组都不会修改。

选项

改变属主和/或属组。使用--reference,改变FILE的属主和属组为RFILE。

-c, --changes

                类似-v,但是只在发生改变时展示信息。

-f, --silent, --quiet

                折叠大部分错误消息。

-v, --verbose

                为每个操作的文件输出诊断信息。

--dereference

                对每个软链接的所指生效(这是默认值),而不是软链接本身。

-h, --no-dereference

                对软链接生效,而不是指向的文件(只在能改变软链接所属的系统中有效)。

--from=CURRENT_OWNER:CURRENT_GROUP

                只对当前的属主和属组匹配的文件生效。可以省略其中一个,在这种情况下,省                    略的属性不需要匹配。

--no-preserve-root

                不特殊处理 '/' (默认)。

--preserve-root

                无法对 '/' 进行递归操作。

-R, --recursive

                递归操作文件和目录。

当指定了 -R 选项时,以下选项修改层次结构的遍历方式。如果指定多于一个选项,只有最后一个生效。

-H        如果一个命令行参数是目录的软链接,就遍历它。

-L        遍历每一个软链接到遇到的目录。

-P        不遍历任何软链接(默认)

--help           展示帮助文档并退出

--version       输出版本信息并退出 

使用示例:

[root@server dir]# ll
总用量 20
-rwxr-xr-x 2 root root  363 12月  2 16:36 a
lrwxrwxrwx 2 root root    4 12月  2 13:54 dir1 -> dir2
drwxr-xr-x 2 root root 4096 12月  2 14:08 dir2
drwxr-xr-x 3 root root 4096 12月  1 16:11 dir3
lrwxrwxrwx 2 root root    4 12月  2 13:54 dir4 -> dir2
-rwxr-xr-x 1 root root    4 12月  1 18:08 file2
-rwxr-xr-x 1 root root    0 11月 28 15:12 file3
-rwxr-xr-x 1 root root    0 11月 29 14:41 file4
-rwxr-xr-x 1 root root  164 11月 28 16:41 file.zip

## 配置属主为test
[root@server dir]# chown test file2
[root@server dir]# ll
总用量 20
-rwxr-xr-x 2 root root  363 12月  2 16:36 a
lrwxrwxrwx 2 root root    4 12月  2 13:54 dir1 -> dir2
drwxr-xr-x 2 root root 4096 12月  2 14:08 dir2
drwxr-xr-x 3 root root 4096 12月  1 16:11 dir3
lrwxrwxrwx 2 root root    4 12月  2 13:54 dir4 -> dir2
-rwxr-xr-x 1 test root    4 12月  1 18:08 file2
-rwxr-xr-x 1 root root    0 11月 28 15:12 file3
-rwxr-xr-x 1 root root    0 11月 29 14:41 file4
-rwxr-xr-x 1 root root  164 11月 28 16:41 file.zip

## 配置属主和属组为test
[root@server dir]# chown test:test file3
[root@server dir]# ll
总用量 20
-rwxr-xr-x 2 root root  363 12月  2 16:36 a
lrwxrwxrwx 2 root root    4 12月  2 13:54 dir1 -> dir2
drwxr-xr-x 2 root root 4096 12月  2 14:08 dir2
drwxr-xr-x 3 root root 4096 12月  1 16:11 dir3
lrwxrwxrwx 2 root root    4 12月  2 13:54 dir4 -> dir2
-rwxr-xr-x 1 test root    4 12月  1 18:08 file2
-rwxr-xr-x 1 test test    0 11月 28 15:12 file3
-rwxr-xr-x 1 root root    0 11月 29 14:41 file4
-rwxr-xr-x 1 root root  164 11月 28 16:41 file.zip

## 配置dir目录下的所有文件的属主为test
[root@server dir]# chown -R test ~/dir
[root@server dir]# ll
总用量 20
-rwxr-xr-x 2 test root  363 12月  2 16:36 a
lrwxrwxrwx 2 test root    4 12月  2 13:54 dir1 -> dir2
drwxr-xr-x 2 test root 4096 12月  2 14:08 dir2
drwxr-xr-x 3 test root 4096 12月  1 16:11 dir3
lrwxrwxrwx 2 test root    4 12月  2 13:54 dir4 -> dir2
-rwxr-xr-x 1 test root    4 12月  1 18:08 file2
-rwxr-xr-x 1 test test    0 11月 28 15:12 file3
-rwxr-xr-x 1 test root    0 11月 29 14:41 file4
-rwxr-xr-x 1 test root  164 11月 28 16:41 file.zip
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐