Linux命令之chown
chown [选项] … [OWNER][:[GROUP]] FILE …
chown [选项] … --reference=RFILE FILE …
chown更改每个给定文件的用户和/或组的所有权。如果仅给出所有者(用户名或UID),则该用户成为每个给定文件的所有者,并且不更改文件的组。如果所有者后跟冒号和组名(或GID),并且它们之间没有空格,则文件的组所有权也会更改。如果所有者后跟冒号但没有组名(或GID),则该用户将成为文件的所有者,并且文件的组将改为该用户的登录组。如果给出冒号和组名(或GID),但省略所有者,则只更改文件的组,这种情况下chown与chgrp执行相同功能。如果仅有冒号,或为空,则不更改所有者和组。
(1).选项
1 2 3 4 5 6 7 8 9 10 11 12 | -c,--changes 类似verbose,但只有在更改时报告 -f,--silent,--quiet 不列出大多数错误信息 -v,--verbose 为每个处理的文件输出诊断信息 --derference 影响每个符号链接的引用(这是默认值),而不是符号链接本身 -h,--no-dereference 影响符号链接本身而不是任何引用的文件(仅在可以更改符号链接的所有权的系统上有用) -- from =CURRENT_OWNER:CURRENT_GROUP 如果文件当前的所有者和/或组与CURRENT_OWNER和/或CURRENT_GROUP匹配时执行更改。两个都可以省略,省略的属性不需要匹配。 --no-preserve-root 要特别对待’/’(根目录?)(默认) --preserve-root 无法以’/’(根目录?)递归操作 --reference=RFILE 使用RFILE的所有者和组而不是指定OWNER:GROUP值 --help 打印帮助信息并退出 --version 打印版本信息并退出 -R,--recursive 以递归方式操作文件和目录 |
当-R选项被指定时,以下选项修改了如何遍历层次结构。如果指定了多个选项,只有最后一个生效。
1 2 3 | -H 如果一个命令行的参数是符号链接,遍历它 -L 遍历目录里遇到的每一个符号链接 -P 不要遍历任何符号链接(默认) |
(2).实例
更改文件所有者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 15:41 1.txt -rw-r--r--. 1 root root 0 11月 9 15:41 2.txt -rw-r--r--. 1 root root 0 11月 9 15:41 3.txt [root@xuexi ~]# chown -v xf newDir/Dir/* //*表示所有文件 changed ownership of "newDir/Dir/1.txt" from root to xf changed ownership of "newDir/Dir/2.txt" from root to xf changed ownership of "newDir/Dir/3.txt" from root to xf [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 xf root 0 11月 9 15:41 1.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 2.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 3.txt [root@xuexi ~]# chown -v root newDir/Dir/{1.txt,2.txt} //多个文件用逗号隔开 changed ownership of "newDir/Dir/1.txt" from xf to root changed ownership of "newDir/Dir/2.txt" from xf to root [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 15:41 1.txt -rw-r--r--. 1 root root 0 11月 9 15:41 2.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 3.txt |
递归更改文件夹及其下文件的所有者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@xuexi ~]# ls -l newDir/ 总用量 0 drwxr-xr-x. 2 root root 45 11月 9 15:41 Dir [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 15:41 1.txt -rw-r--r--. 1 root root 0 11月 9 15:41 2.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 3.txt [root@xuexi ~]# chown -vR xf newDir/Dir changed ownership of "newDir/Dir/1.txt" from root to xf changed ownership of "newDir/Dir/2.txt" from root to xf "newDir/Dir/3.txt" 的所有者已保留为xf changed ownership of "newDir/Dir" from root to xf [root@xuexi ~]# ls -l newDir/ 总用量 0 drwxr-xr-x. 2 xf root 45 11月 9 15:41 Dir [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 xf root 0 11月 9 15:41 1.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 2.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 3.txt |
同时更改文件的所有者和所属组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@xuexi ~]# ls -l newDir/ 总用量 0 drwxr-xr-x. 2 xf root 45 11月 9 15:41 Dir [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 xf root 0 11月 9 15:41 1.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 2.txt -rw-r--r--. 1 xf root 0 11月 9 15:41 3.txt [root@xuexi ~]# chown -vR root:xf newDir/Dir changed ownership of "newDir/Dir/1.txt" from xf:root to root:xf changed ownership of "newDir/Dir/2.txt" from xf:root to root:xf changed ownership of "newDir/Dir/3.txt" from xf:root to root:xf changed ownership of "newDir/Dir" from xf:root to root:xf [root@xuexi ~]# ls -l newDir/ 总用量 0 drwxr-xr-x. 2 root xf 45 11月 9 15:41 Dir [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 root xf 0 11月 9 15:41 1.txt -rw-r--r--. 1 root xf 0 11月 9 15:41 2.txt -rw-r--r--. 1 root xf 0 11月 9 15:41 3.txt |
只更改文件的所属组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@xuexi ~]# ls -l newDir/ 总用量 0 drwxr-xr-x. 2 root xf 45 11月 9 15:41 Dir [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 root xf 0 11月 9 15:41 1.txt -rw-r--r--. 1 root xf 0 11月 9 15:41 2.txt -rw-r--r--. 1 root xf 0 11月 9 15:41 3.txt [root@xuexi ~]# chown -vR :root newDir/Dir changed ownership of "newDir/Dir/1.txt" from root:xf to :root changed ownership of "newDir/Dir/2.txt" from root:xf to :root changed ownership of "newDir/Dir/3.txt" from root:xf to :root changed ownership of "newDir/Dir" from root:xf to :root [root@xuexi ~]# ls -l newDir/ 总用量 0 drwxr-xr-x. 2 root root 45 11月 9 15:41 Dir [root@xuexi ~]# ls -l newDir/Dir/ 总用量 0 -rw-r--r--. 1 root root 0 11月 9 15:41 1.txt -rw-r--r--. 1 root root 0 11月 9 15:41 2.txt -rw-r--r--. 1 root root 0 11月 9 15:41 3.txt |
(3).扩展
chgrp [GROUP] FILE
一样可以修改所属组
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性