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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律