#Bash 的 chown 命令
1chown [OPTION]... [OWNER][:[GROUP]] FILE... 2
功能
修改文件的所有者和所属组。
类型
可执行文件(/usr/bin/chown),属于 coreutils。
参数
OPTION选项:-c,--changes- 仅对发生变化的文件打印详细信息-f,--silent,--quiet- 忽略大部分错误信息-v,--verbose- 打印详细信息--dereference- 影响符号链接引用的源文件,而非符号链接本身(默认)-h,--no-dereference- 影响符号链接本身,而不是其引用的源文件--from=CURRENT_OWNER:CURRENT_GROUP- 仅当文件的所有者和所属主是CURRENT_OWNER和CURRENT_GROUP才进行修改--no-preserve-root- 不保护/目录--preserve-root- 保护/目录--reference=RFILE- 引用RFILE文件的配置-R,--recursive- 对目录进行递归操作-H- 使用-R选项进行递归操作时,指向目录的符号链接只有作为命令行参数时才遍历-L- 使用-R选项进行递归操作时,遍历所有指向目录的符号链接-P- 使用-R选项进行递归操作时,不遍历任何符号链接(默认)--help- 显示帮助--version- 显示版本
OWNER- 要设为的所有者GROUP- 要设为的所属组FILE- 文件列表
#示例
用户名或组名
1$ chown planc file.txt # 将 file 的所有者设为 planc 2$ chown :primers file.txt # 将 file 的所属组设为 primers 3$ chown planc:primers file.txt # 将 file 的所有者设为 planc 所属组设为 primers 4
用 ID 或组 ID
1$ chown 1001 file.txt # 将 file 的所有者 ID 设为 1001 2$ chown :1001 file.txt # 将 file 的所属组 ID 设为 1001 3$ chown 1001:1001 file.txt # 将 file 的所有者 ID 设为 1001 所属组 ID 设为 1001 4
其它
1$ chown --reference=2.txt 1.txt # 将 1.txt 的所有者和所属组设为和 2.txt 一样 2$ chown planc:primers -R dir # 递归设置目录 dir 中的文件 3
修改根目录(危险!)
1$ chown planc --no-preserve-root -R / # 危险!千万不要执行此命令! 2
修改系统关键文件的所有者可能导致系统无法启动,请谨慎操作。
#相关命令
| 命令 | 说明 |
|---|---|
| chgrp | 修改文件的所属组 |
| chmod | 修改文件的访问权限 |
#推荐阅读
#手册
显示
1CHOWN(1) User Commands CHOWN(1) 2 3NAME 4 chown - change file owner and group 5 6SYNOPSIS 7 chown [OPTION]... [OWNER][:[GROUP]] FILE... 8 chown [OPTION]... --reference=RFILE FILE... 9 10DESCRIPTION 11 This manual page documents the GNU version of chown. chown changes the 12 user and/or group ownership of each given file. If only an owner (a 13 user name or numeric user ID) is given, that user is made the owner of 14 each given file, and the files' group is not changed. If the owner is 15 followed by a colon and a group name (or numeric group ID), with no 16 spaces between them, the group ownership of the files is changed as 17 well. If a colon but no group name follows the user name, that user is 18 made the owner of the files and the group of the files is changed to 19 that user's login group. If the colon and group are given, but the 20 owner is omitted, only the group of the files is changed; in this case, 21 chown performs the same function as chgrp. If only a colon is given, 22 or if the entire operand is empty, neither the owner nor the group is 23 changed. 24 25OPTIONS 26 Change the owner and/or group of each FILE to OWNER and/or GROUP. With 27 --reference, change the owner and group of each FILE to those of RFILE. 28 29 -c, --changes 30 like verbose but report only when a change is made 31 32 -f, --silent, --quiet 33 suppress most error messages 34 35 -v, --verbose 36 output a diagnostic for every file processed 37 38 --dereference 39 affect the referent of each symbolic link (this is the default), 40 rather than the symbolic link itself 41 42 -h, --no-dereference 43 affect symbolic links instead of any referenced file (useful 44 only on systems that can change the ownership of a symlink) 45 46 --from=CURRENT_OWNER:CURRENT_GROUP 47 change the owner and/or group of each file only if its current 48 owner and/or group match those specified here. Either may be 49 omitted, in which case a match is not required for the omitted 50 attribute 51 52 --no-preserve-root 53 do not treat '/' specially (the default) 54 55 --preserve-root 56 fail to operate recursively on '/' 57 58 --reference=RFILE 59 use RFILE's owner and group rather than specifying OWNER:GROUP 60 values. RFILE is always dereferenced. 61 62 -R, --recursive 63 operate on files and directories recursively 64 65 The following options modify how a hierarchy is traversed when the -R 66 option is also specified. If more than one is specified, only the fi‐ 67 nal one takes effect. 68 69 -H if a command line argument is a symbolic link to a directory, 70 traverse it 71 72 -L traverse every symbolic link to a directory encountered 73 74 -P do not traverse any symbolic links (default) 75 76 --help display this help and exit 77 78 --version 79 output version information and exit 80 81 Owner is unchanged if missing. Group is unchanged if missing, but 82 changed to login group if implied by a ':' following a symbolic OWNER. 83 OWNER and GROUP may be numeric as well as symbolic. 84 85EXAMPLES 86 chown root /u 87 Change the owner of /u to "root". 88 89 chown root:staff /u 90 Likewise, but also change its group to "staff". 91 92 chown -hR root /u 93 Change the owner of /u and subfiles to "root". 94 95AUTHOR 96 Written by David MacKenzie and Jim Meyering. 97 98REPORTING BUGS 99 GNU coreutils online help: <https://www.gnu.org/software/coreutils/> 100 Report any translation bugs to <https://translationproject.org/team/> 101 102COPYRIGHT 103 Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU 104 GPL version 3 or later <https://gnu.org/licenses/gpl.html>. 105 This is free software: you are free to change and redistribute it. 106 There is NO WARRANTY, to the extent permitted by law. 107 108SEE ALSO 109 chown(2) 110 111 Full documentation <https://www.gnu.org/software/coreutils/chown> 112 or available locally via: info '(coreutils) chown invocation' 113 114GNU coreutils 9.4 April 2024 CHOWN(1) 115
《Bash 的 chown 命令》 是转载文章,点击查看原文。