chgrp — 變更檔案或目錄的所屬群組

變更檔案或目錄的所屬群組。

語法

chgrp [選項] 群組 檔案...

常用選項

選項 說明 範例
-R, --recursive 遞迴套用到目錄及其所有內容 chgrp -R webteam /var/www/
-v, --verbose 顯示每個已處理的檔案 chgrp -v developers file
-c, --changes 只顯示有實際變更的檔案 chgrp -c developers file
--reference=檔 使用參考檔的群組 chgrp --reference=ref.txt file

使用範例

範例 1:變更檔案的群組

將 project.py 的群組改為 developers。

$ sudo chgrp developers project.py
範例 2:遞迴變更目錄群組

讓 webteam 群組的所有成員都可以操作 /var/www/html。

$ sudo chgrp -R webteam /var/www/html/
範例 3:等效的 chown 寫法

chgrp developers file 等同於 chown :developers file。

$ sudo chown :developers project.py   # 和 chgrp 相同效果

常見錯誤與排錯

chgrp: invalid group: 'groupname'
原因
指定的群組不存在。
解法
用 getent group 確認群組是否存在,或先用 groupadd 建立群組。

延伸閱讀