1. chgrp 命令说明
chgrp:更改文件与目录的所属组,比 chown 更专注,哈哈。基本信息如下:
Usage: chgrp [OPTION]... GROUP FILE...
or: chgrp [OPTION]... --reference=RFILE FILE...
Change the group of each FILE to GROUP.
With --reference, change the group of each FILE to that of RFILE.
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file
(useful only on systems that can change the
ownership of a symlink)
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's group rather than specifying a
GROUP value
-R, --recursive operate on files and directories recursively
The following options modify how a hierarchy is traversed when the -R
option is also specified. If more than one is specified, only the final
one takes effect.
-H if a command line argument is a symbolic link
to a directory, traverse it
-L traverse every symbolic link to a directory
encountered
-P do not traverse any symbolic links (default)
--help display this help and exit
--version output version information and exit
Examples:
chgrp staff /u Change the group of /u to "staff".
chgrp -hR staff /u Change the group of /u and subfiles to "staff".
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'chgrp invocation'
参数:
选项 | 作用 |
---|---|
-c | 只有真正更改时才会显示信息 |
-f | 忽略错误信息 |
-h | 修复符号链接 |
-v | 显示详细的处理信息 |
-R | 处理文件夹及文件夹中所有文件、子文件夹 |
-H | 配合 -R ,如果命令行参数是指向目录的符号链接,则遍历它 |
-L | 配合 -R ,遍历遇到的每个指向目录的符号链接 |
-P | 配合 -R ,不遍历任何符号链接(默认) |
可以看到与 chown 是一样的。
2. chgrp 命令语法
chgrp [OPTION]... GROUP FILE...
3. chgrp 命令示例
3.1 使用组名称或GID
可以使用组名或 GID 两种方式修改文件的属组
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2 0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test1 30 Jun 3 12:44 bbb
[root@localhost aaa]# chgrp test1 a.txt
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test1 0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test1 30 Jun 3 12:44 bbb
[root@localhost aaa]# id test2
uid=1015(test2) gid=1015(test2) groups=1015(test2)
[root@localhost aaa]# chgrp 1015 a.txt
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2 0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test1 30 Jun 3 12:44 bbb
3.2 -R(迭代修改文件夹属组)
想将文件夹内容的属组一起修改时,需要加 -R
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2 0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test1 30 Jun 3 12:44 bbb
[root@localhost aaa]# cd bbb/
[root@localhost bbb]# ll
total 0
-rwxrwxrwx. 1 test1 test1 0 Jun 3 12:44 1.txt
drwxrwxrwx. 2 test1 test1 6 Jun 3 12:38 ccc
[root@localhost bbb]# cd ..
[root@localhost aaa]# chgrp -R test2 bbb/
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2 0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test2 30 Jun 3 12:44 bbb
[root@localhost aaa]# cd bbb/
[root@localhost bbb]# ll
total 0
-rwxrwxrwx. 1 test1 test2 0 Jun 3 12:44 1.txt
drwxrwxrwx. 2 test1 test2 6 Jun 3 12:38 ccc
[root@localhost bbb]#
4. 总结
chgrp:更改文件与目录的所属组,参数选项、作用、用法与 chown 一样。