功能说明
chgrp命令用来改变文件或目录所属的用户组。其中,组名可以是用户组的id,也可以是用户组的组名。文件名可以 是由空格分开的要改变属组的文件列表,也可以是由通配符描述的文件集合。如果用户不是该文件的文件主或超级用户(root),则不能改变该文件的组。
来自: http://man.linuxde.net/chgrp
常用选项
语法
chgrp(选项)(参数)
选项
必要选项:
-c 当发生改变时输出调试信息
-f 不显示错误信息
-R 处理指定目录以及其子目录下的所有文件
-v 运行时显示详细的处理信息
–dereference 作用于符号链接的指向,而不是符号链接本身
–no-dereference 作用于符号链接本身
选择选项:
–reference=<文件或者目录>
–help 显示帮助信息
–version 显示版本信息
参数
组:指定新工作名称;
文件:指定要改变所属组的文件列表。多个文件或者目录之间使用空格隔开。
使用实例
1.改变文件的群组属性
scarlett@scarlett-X550VXK:~/file$ sudo chgrp root 1.txt
[sudo] scarlett 的密码:
scarlett@scarlett-X550VXK:~/file$ ls -l
总用量 48
-r--rwx--x 2 root root 2099 10月 10 16:17 10.txt
-rw-rw-r-- 1 root root 1121 10月 10 16:17 15.txt
-rw-rw-r-- 1 root root 1419 10月 10 16:17 16.txt
-rw-rw-r-- 2 scarlett root 0 10月 10 20:35 1.txt
-rw-rw-r-- 1 root root 419 10月 10 16:17 20.txt
-rw-rw-r-- 1 root root 1124 10月 10 16:17 21.txt
-rw-rw-r-- 1 root root 540 10月 10 16:17 22.txt
-rw-rw-r-- 1 root root 1168 10月 10 16:17 23.txt
-rw-rw-r-- 1 root root 9113 10月 10 16:17 24.txt
-rw-rw-r-- 1 root root 42 10月 10 16:17 2.txt
-rw-rw-r-- 1 root root 1854 10月 10 16:17 5.txt
2.根据指定文件改变指定目录下所有文件的所属群组
scarlett@scarlett-X550VXK:~/file$ ls -l
总用量 48
-r--rwx--x 2 root root 2099 10月 10 16:17 10.txt
-rw-rw-r-- 1 root root 1121 10月 10 16:17 15.txt
-rw-rw-r-- 1 root root 1419 10月 10 16:17 16.txt
-rw-rw-r-- 2 scarlett scarlett 0 10月 10 20:35 1.txt
-rw-rw-r-- 1 root root 419 10月 10 16:17 20.txt
-rw-rw-r-- 1 root root 1124 10月 10 16:17 21.txt
-rw-rw-r-- 1 root root 540 10月 10 16:17 22.txt
-rw-rw-r-- 1 root root 1168 10月 10 16:17 23.txt
-rw-rw-r-- 1 root root 9113 10月 10 16:17 24.txt
-rw-rw-r-- 1 root root 42 10月 10 16:17 2.txt
-rw-rw-r-- 1 root root 1854 10月 10 16:17 5.txt
scarlett@scarlett-X550VXK:~/file$ sudo chgrp --reference=1.txt -R ./
scarlett@scarlett-X550VXK:~/file$ ls -l
总用量 48
-r--rwx--x 2 root scarlett 2099 10月 10 16:17 10.txt
-rw-rw-r-- 1 root scarlett 1121 10月 10 16:17 15.txt
-rw-rw-r-- 1 root scarlett 1419 10月 10 16:17 16.txt
-rw-rw-r-- 2 scarlett scarlett 0 10月 10 20:35 1.txt
-rw-rw-r-- 1 root scarlett 419 10月 10 16:17 20.txt
-rw-rw-r-- 1 root scarlett 1124 10月 10 16:17 21.txt
-rw-rw-r-- 1 root scarlett 540 10月 10 16:17 22.txt
-rw-rw-r-- 1 root scarlett 1168 10月 10 16:17 23.txt
-rw-rw-r-- 1 root scarlett 9113 10月 10 16:17 24.txt
-rw-rw-r-- 1 root scarlett 42 10月 10 16:17 2.txt
-rw-rw-r-- 1 root scarlett 1854 10月 10 16:17 5.txt
3.通过群组识别码改变文件属组属性。
通过群组识别码改变文件群组属性,100为users群组的识别码,具体群组和群组识别码可以去/etc/group文件中查看
scarlett@scarlett-X550VXK:~/file$ ls -l
总用量 48
-r--rwx--x 2 root scarlett 2099 10月 10 16:17 10.txt
-rw-rw-r-- 1 root scarlett 1121 10月 10 16:17 15.txt
-rw-rw-r-- 1 root scarlett 1419 10月 10 16:17 16.txt
-rw-rw-r-- 2 scarlett scarlett 0 10月 10 20:35 1.txt
-rw-rw-r-- 1 root scarlett 419 10月 10 16:17 20.txt
-rw-rw-r-- 1 root scarlett 1124 10月 10 16:17 21.txt
-rw-rw-r-- 1 root scarlett 540 10月 10 16:17 22.txt
-rw-rw-r-- 1 root scarlett 1168 10月 10 16:17 23.txt
-rw-rw-r-- 1 root scarlett 9113 10月 10 16:17 24.txt
-rw-rw-r-- 1 root scarlett 42 10月 10 16:17 2.txt
-rw-rw-r-- 1 root scarlett 1854 10月 10 16:17 5.txt
scarlett@scarlett-X550VXK:~/file$ sudo chgrp 100 -R ./
scarlett@scarlett-X550VXK:~/file$ ls -l
总用量 48
-r--rwx--x 2 root users 2099 10月 10 16:17 10.txt
-rw-rw-r-- 1 root users 1121 10月 10 16:17 15.txt
-rw-rw-r-- 1 root users 1419 10月 10 16:17 16.txt
-rw-rw-r-- 2 scarlett users 0 10月 10 20:35 1.txt
-rw-rw-r-- 1 root users 419 10月 10 16:17 20.txt
-rw-rw-r-- 1 root users 1124 10月 10 16:17 21.txt
-rw-rw-r-- 1 root users 540 10月 10 16:17 22.txt
-rw-rw-r-- 1 root users 1168 10月 10 16:17 23.txt
-rw-rw-r-- 1 root users 9113 10月 10 16:17 24.txt
-rw-rw-r-- 1 root users 42 10月 10 16:17 2.txt
-rw-rw-r-- 1 root users 1854 10月 10 16:17 5.txt