linux系统中chgrp命令

linux系统中chgrp命令用户改变文件或者目录的所属组

chgrp 为 change group的缩写

用法: chgrp 【选项】 文件/目录

1、创建测试文件

[root@linuxprobe test]# ls
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# touch a.txt b.txt
[root@linuxprobe test]# su - liujiaxin01
Last login: Tue Oct 20 21:29:13 CST 2020 on pts/0
[liujiaxin01@linuxprobe ~]$ cd /home/test/
[liujiaxin01@linuxprobe test]$ whoami
liujiaxin01
[liujiaxin01@linuxprobe test]$ touch c.txt d.txt
[liujiaxin01@linuxprobe test]$ exit
logout
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        root        0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        root        0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 d.txt

2、直接用法

[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        root        0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        root        0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 d.txt
[root@linuxprobe test]# chgrp liujiaxin01 a.txt  ## 将a.txt的所属组改为刘家鑫01
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        liujiaxin01 0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        root        0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 d.txt
[root@linuxprobe test]# chgrp root c.txt   ## 将c.txt的所属组改为root
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        liujiaxin01 0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        root        0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 root        0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 d.txt

3、可以直接使用组ID

[root@linuxprobe test]# id root  ## 查看组ID
uid=0(root) gid=0(root) groups=0(root)
[root@linuxprobe test]# id liujiaxin01  ##查看组ID
uid=1001(liujiaxin01) gid=1001(liujiaxin01) groups=1001(liujiaxin01)
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        liujiaxin01 0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        root        0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 root        0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 liujiaxin01 0 Oct 20 21:42 d.txt
[root@linuxprobe test]# chgrp 0 d.txt  ## 将d.txt的所属组改为root
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        liujiaxin01 0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        root        0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 root        0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 root        0 Oct 20 21:42 d.txt
[root@linuxprobe test]# chgrp 1001 b.txt  ## 将b.txt的组ID改为liujiaxin01
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root        liujiaxin01 0 Oct 20 21:42 a.txt
-rw-r--r--. 1 root        liujiaxin01 0 Oct 20 21:42 b.txt
-rw-rw-r--. 1 liujiaxin01 root        0 Oct 20 21:42 c.txt
-rw-rw-r--. 1 liujiaxin01 root        0 Oct 20 21:42 d.txt

4、使用 -R 参数进行递归操作

[root@linuxprobe test]# ls
[root@linuxprobe test]# mkdir test01 test02
[root@linuxprobe test]# touch test01/a.txt
[root@linuxprobe test]# touch test02/b.txt
[root@linuxprobe test]# ll
total 0
drwxr-xr-x. 2 root root 18 Oct 20 21:51 test01
drwxr-xr-x. 2 root root 18 Oct 20 21:51 test02
[root@linuxprobe test]# ll test01/a.txt
-rw-r--r--. 1 root root 0 Oct 20 21:51 test01/a.txt
[root@linuxprobe test]# ll test02/b.txt
-rw-r--r--. 1 root root 0 Oct 20 21:51 test02/b.txt
[root@linuxprobe test]# chgrp liujiaxin01 test01/
[root@linuxprobe test]# chgrp -R liujiaxin01 test02/
[root@linuxprobe test]# ll
total 0
drwxr-xr-x. 2 root liujiaxin01 18 Oct 20 21:51 test01
drwxr-xr-x. 2 root liujiaxin01 18 Oct 20 21:51 test02
[root@linuxprobe test]# ll test01/a.txt
-rw-r--r--. 1 root root 0 Oct 20 21:51 test01/a.txt
[root@linuxprobe test]# ll test02/b.txt   ## 加-R参数后,目录下的文件所属组同时改变
-rw-r--r--. 1 root liujiaxin01 0 Oct 20 21:51 test02/b.txt

5、--reference参数设置参考文件所属组修改文件或者目录所属组

[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root liujiaxin01 0 Oct 20 22:08 a.txt
-rw-r--r--. 1 root root        0 Oct 20 22:08 b.txt
[root@linuxprobe test]# chgrp --reference=b.txt a.txt  ## 将a.txt的所属组改为b.txt的所属组
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 20 22:08 a.txt
-rw-r--r--. 1 root root 0 Oct 20 22:08 b.txt
[root@linuxprobe test]# chgrp liujiaxin01 b.txt
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root root        0 Oct 20 22:08 a.txt
-rw-r--r--. 1 root liujiaxin01 0 Oct 20 22:08 b.txt
[root@linuxprobe test]# chgrp --reference=b.txt a.txt  ## 同上
[root@linuxprobe test]# ll
total 0
-rw-r--r--. 1 root liujiaxin01 0 Oct 20 22:08 a.txt
-rw-r--r--. 1 root liujiaxin01 0 Oct 20 22:08 b.txt
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13849446.html