chgrp [選項] … GROUP FILE …
chgrp [選項] … --reference=RFILE FILE …
chgrp命令是用來改變文件的組所有權。將改變每一個FILE的所屬組為GROUP。使用--reference時,將改變每個FILE的所屬組為RFILE的所屬組。
(1).選項
-c,--changes 類似verbose,但僅在進行更改時報告。 -f,--silent,--quiet 不列出大多數錯誤信息 -v,--verbose 為每個處理的文件輸出診斷信息。 --dereference 影響每個符號鏈接的引用(這是默認值),而不是符號鏈接本身 -h,--no-derference 影響符號鏈接本身而不是任何引用的文件(僅在可以更改符號鏈接的所有權的系統上有用) --no-preserve-root 要特別對待’/’(根目錄?)(默認) --preserve-root 無法以’/’(根目錄?)遞歸操作 --reference=RFILE 使用RFILE的組而不是指定GROUP值 --help 顯示幫助並退出 --version 打印版本信息並退出 -R,--recursive 以遞歸方式操作文件和目錄
當-R選項被指定時,以下選項修改了如何遍歷層次結構。如果指定了多個選項,只有最后一個生效。
-H 如果一個命令行的參數是符號鏈接,遍歷它 -L 遍歷目錄里遇到的每一個符號鏈接 -P 不要遍歷任何符號鏈接(默認)
(2).實例
更改文件1.txt的用戶組
[root@xuexi xf]# ls -l 總用量 0 -rw-rw-r--. 1 xf xf 0 11月 7 11:00 1.txt drwxr-xr-x. 2 xf xf 6 9月 6 11:43 公共 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 模板 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 視頻 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 圖片 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 文檔 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 下載 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 音樂 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 桌面 [root@xuexi xf]# chgrp -v root 1.txt changed group of "1.txt" from xf to root [root@xuexi xf]# ls -l 1.txt -rw-rw-r--. 1 xf root 0 11月 7 11:00 1.txt
參照文件1.txt的用戶組更改文件2.txt的用戶組
[root@xuexi xf]# ls -l 總用量 0 -rw-rw-r--. 1 xf root 0 11月 7 11:00 1.txt -rw-rw-r--. 1 xf xf 0 11月 7 11:04 2.txt drwxr-xr-x. 2 xf xf 6 9月 6 11:43 公共 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 模板 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 視頻 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 圖片 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 文檔 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 下載 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 音樂 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 桌面 [root@xuexi xf]# chgrp -v --reference=1.txt 2.txt changed group of "2.txt" from xf to root [root@xuexi xf]# ls -l 2.txt -rw-rw-r--. 1 xf root 0 11月 7 11:04 2.txt
遞歸指定目錄,更改其下所有用戶組,以及其本身的用戶組
[root@xuexi xf]# ls -l 總用量 0 drwxrwxr-x. 3 xf xf 47 11月 7 11:25 newDir drwxr-xr-x. 2 xf xf 6 9月 6 11:43 公共 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 模板 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 視頻 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 圖片 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 文檔 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 下載 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 音樂 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 桌面 [root@xuexi xf]# ls -l newDir/ 總用量 0 -rw-rw-r--. 1 xf xf 0 11月 7 11:25 3.txt -rw-rw-r--. 1 xf xf 0 11月 7 11:25 4.txt drwxrwxr-x. 2 xf xf 32 11月 7 11:25 newDir1 [root@xuexi xf]# ls -l newDir/newDir1/ 總用量 0 -rw-rw-r--. 1 xf xf 0 11月 7 11:25 5.txt -rw-rw-r--. 1 xf xf 0 11月 7 11:25 6.txt [root@xuexi newDir1]# cd /home/xf [root@xuexi xf]# chgrp -vR root newDir/ changed group of "newDir/3.txt" from xf to root changed group of "newDir/4.txt" from xf to root changed group of "newDir/newDir1/5.txt" from xf to root changed group of "newDir/newDir1/6.txt" from xf to root changed group of "newDir/newDir1" from xf to root changed group of "newDir/" from xf to root [root@xuexi xf]# ls -l 總用量 0 drwxrwxr-x. 3 xf root 47 11月 7 11:25 newDir drwxr-xr-x. 2 xf xf 6 9月 6 11:43 公共 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 模板 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 視頻 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 圖片 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 文檔 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 下載 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 音樂 drwxr-xr-x. 2 xf xf 6 9月 6 11:43 桌面 [root@xuexi xf]# ls -l newDir/ 總用量 0 -rw-rw-r--. 1 xf root 0 11月 7 11:25 3.txt -rw-rw-r--. 1 xf root 0 11月 7 11:25 4.txt drwxrwxr-x. 2 xf root 32 11月 7 11:25 newDir1 [root@xuexi xf]# ls -l newDir/newDir1/ 總用量 0 -rw-rw-r--. 1 xf root 0 11月 7 11:25 5.txt -rw-rw-r--. 1 xf root 0 11月 7 11:25 6.txt
也可以用群組識別碼來代替組名,詳細可看/etc/group
[root@xuexi xf]# ls -l 7.txt -rw-rw-r--. 1 xf xf 0 11月 7 11:46 7.txt [root@xuexi xf]# chgrp -v 5 7.txt changed group of "7.txt" from xf to 5 [root@xuexi xf]# ls -l 7.txt -rw-rw-r--. 1 xf tty 0 11月 7 11:46 7.txt
群組識別碼
[xf@xuexi ~]$ cat /etc/group root:x:0: bin:x:1: daemon:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mem:x:8: kmem:x:9: wheel:x:10:xf cdrom:x:11: mail:x:12:postfix man:x:15: dialout:x:18: floppy:x:19: games:x:20: tape:x:33: video:x:39: ftp:x:50: lock:x:54: audio:x:63: nobody:x:99: users:x:100: utmp:x:22: utempter:x:35: stapusr:x:156: stapsys:x:157: stapdev:x:158: input:x:999: systemd-journal:x:190: systemd-network:x:192: dbus:x:81: polkitd:x:998: printadmin:x:997: libstoragemgmt:x:996: rpc:x:32: colord:x:995: dip:x:40: cgred:x:994: ssh_keys:x:993: saslauth:x:76: abrt:x:173: setroubleshoot:x:992: rtkit:x:172: chrony:x:991: rpcuser:x:29: nfsnobody:x:65534: kvm:x:36:qemu qemu:x:107: unbound:x:990: gluster:x:989: tss:x:59: libvirt:x:988: usbmuxd:x:113: geoclue:x:987: radvd:x:75: pulse-access:x:986: pulse-rt:x:985: pulse:x:171: gdm:x:42: gnome-initial-setup:x:984: sshd:x:74: avahi:x:70: slocate:x:21: postdrop:x:90: postfix:x:89: ntp:x:38: tcpdump:x:72: xf:x:1000: