一,什么是chgrp
https://man.he.net/man1/chgrp
chgrp 是 Linux/Unix 系统中的一个基础命令,全称是 Change Group(更改组)。它的主要作用是修改文件或目录的所属用户组(Group Ownership)。
在 Linux 的权限管理机制中,每个文件和目录都归属于一个特定的“用户(User)”和一个特定的“用户组(Group)”。chgrp 就是专门用来调整“组”这一属性的工具。
二,参数
[root@localhost ~]# chgrp --help用法:chgrp [选项]... 用户组 文件...chgrp [选项]... --reference=参考文件 文件...功能描述:将每个指定文件的所属组更改为指定的“用户组”。如果使用 --reference 选项,则将文件的所属组更改为与“参考文件”的所属组相同。-c, --changes 类似于 verbose,但仅在发生更改时进行报告-f, --silent, --quiet 抑制大多数错误信息-v, --verbose 对每个处理的文件输出诊断信息 --dereference 影响每个符号链接所指向的目标文件(这是默认行为),而不是符号链接本身-h, --no-dereference 影响符号链接本身,而不是其指向的任何文件(仅在可以更改符号链接所有权的系统上有用) --no-preserve-root 不对 '/' 进行特殊处理(默认行为)--preserve-root 在递归操作时,若对 '/' 进行操作则失败--reference=参考文件 使用参考文件的所属组,而不是指定一个用户组值-R, --recursive 递归地对文件和目录进行操作 当同时指定 -R 选项时,以下选项会修改遍历层级结构的方式。如果指定了多个选项,则只有最后一个生效。-H 如果命令行参数是指向目录的符号链接,则遍历该目录-L 遍历遇到的每一个指向目录的符号链接-P 不遍历任何符号链接(默认行为)--help 显示此帮助信息并退出--version 输出版本信息并退出示例:chgrp staff /u 将 /u 的所属组更改为 "staff"。chgrp -hR staff /u 将 /u 及其子文件的所属组更改为 "staff"。GNU coreutils 在线帮助:http://www.gnu.org/software/coreutils/完整文档请运行:info coreutils 'chgrp invocation'
三,补充
Linux 文件权限(rwx)速查表
| | | |
|---|
| | | cat file.txt |
| | | echo "text" > file.txt |
| | 文件:可作为程序运行;目录:可进入该目录 (cd) | ./script.sh |
| | | rwx |
| | | r-x |
| | | r-- |
| | | r-- |
| | | -w- |
| | | --x |
| | | --- |
| | rwxr-xr-x | |
| | rw-r--r-- | |
| | rwx------ | |
| | rwxrwxrwx | |
| | | ls -l filename |
| | | chmod 755 filename |
| | | chown user filename |
| | | chgrp group filename |
| | | chown user:group filename |
chown 与 chgrp 搭配使用速查表
| | | | |
|---|
| chown | chown 用户名 文件 | chown alice file.txt | |
| chgrp | chgrp 组名 文件 | chgrp dev file.txt | |
| chown | chown 用户名:组名 文件 | chown alice:dev file.txt | |
| chgrp | chgrp --reference=参考文件 目标文件chown --reference=参考文件 目标文件 | chgrp --reference=a.txt b.txt | |
| chown | chown -R 用户名:组名 目录 | chown -R alice:dev /var/www/ | |
| chown | chown 用户名: 文件 | chown alice: file.txt | 注意冒号后留空,系统会自动将组设为 alice 的默认登录组 |
四,例子
1、修改单个文件的所属组场景:将普通文件 test2.m4 的所属组更改为 edu
[root@localhost bz]# chgrp edu test2.m4[root@localhost bz]# lltotal 12drwxr-xr-x 2 root root 4096 Jul 7 17:19 test-rw-r--r-- 1 root edu 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root root 293 Jul 7 17:24 test2.m4.bz2
2、修改目录的所属组,场景:将目录 test 的所属组更改为 edu(仅修改目录本身,不包含内部文件)。
[root@localhost bz]# chgrp edu test[root@localhost bz]# lltotal 12drwxr-xr-x 2 root edu 4096 Jul 7 17:19 test-rw-r--r-- 1 root edu 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root root 293 Jul 7 17:24 test2.m4.bz2
3、递归修改目录下所有内容(最常用)
[root@localhost bz]# ll testtotal 8-rw-r--r-- 1 root root 299 Jul 7 17:18 file.txt-rw-r--r-- 1 root root 299 Jul 7 17:19 new_filename.txt[root@localhost bz]# chgrp -R edu test[root@localhost bz]# ll testtotal 8-rw-r--r-- 1 root edu 299 Jul 7 17:18 file.txt-rw-r--r-- 1 root edu 299 Jul 7 17:19 new_filename.txt
4、 参考现有文件的组进行批量修改
[root@localhost bz]# chgrp --reference=test2.m4 test2.m4.bz2 [root@localhost bz]# lltotal 12drwxr-xr-x 2 root edu 4096 Jul 7 17:19 test-rw-r--r-- 1 root edu 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root edu 293 Jul 7 17:24 test2.m4.bz2
5、同时修改多个文件
[root@localhost bz]# chgrp postgres test2.m4 test2.m4.bz2 [root@localhost bz]# lltotal 12drwxr-xr-x 2 root edu 4096 Jul 7 17:19 test-rw-r--r-- 1 root postgres 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root postgres 293 Jul 7 17:24 test2.m4.bz2
6、显示修改过程(带详细反馈)
[root@localhost bz]# chgrp -c -R edu test2.m4changed group of ‘test2.m4’ from postgres to edu[root@localhost bz]# lltotal 12drwxr-xr-x 2 root edu 4096 Jul 7 17:19 test-rw-r--r-- 1 root edu 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root postgres 293 Jul 7 17:24 test2.m4.bz2
7、处理符号链接(软链接)默认情况下,chgrp 会修改符号链接指向的目标文件的组。如果你只想修改符号链接本身的组(需系统支持),可以加上 -h 参数
[root@localhost bz]# ln -s test2.m4 test3.m4[root@localhost bz]# [root@localhost bz]# lltotal 12drwxr-xr-x 2 root edu 4096 Jul 7 17:19 test-rw-r--r-- 1 root edu 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root postgres 293 Jul 7 17:24 test2.m4.bz2lrwxrwxrwx 1 root root 8 Jul 26 00:25 test3.m4 -> test2.m4[root@localhost bz]# chgrp -h edu test3.m4 [root@localhost bz]# lltotal 12drwxr-xr-x 2 root edu 4096 Jul 7 17:19 test-rw-r--r-- 1 root edu 299 Jul 7 17:12 test2.m4-rw-r--r-- 1 root postgres 293 Jul 7 17:24 test2.m4.bz2lrwxrwxrwx 1 root edu 8 Jul 26 00:25 test3.m4 -> test2.m4
五,总结
chgrp由David MacKenzie和Jim Meyering撰写,作用和chown类似。
报告错误
GNU核心工具在线帮助:<https://www.gnu.org/software/coreutils/> 如发现任何翻译错误,请向<https://translationproject.org/team/>报告
版权声明 版权所有 (C) 2020 自由软件基金会股份有限公司。许可证 GPLv3+:
GNU GPL 版本 3 或更高版本 <https://gnu.org/licenses/gpl.html>。这是自由软件:您可以自由修改和重新分发。在法律允许的范围内,不提供任何保证。
另见 chown(1), chown(2)
完整文档<https://www.gnu.org/software/coreutils/chgrp>, 或可通过以下命令在本地获取:info '(coreutils) chgrp invocation'
最后的最后(Last but not least),欢迎交流:
关注公众号留言,或者在下方直接留言: