Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux chgrp 命令詳解:變更檔案與目錄的所屬群組

chgrp 是 change group 的縮寫,專門用來調整檔案或目錄的所屬群組。需要特別留意的是,欲指定的群組必須已經存在於 /etc/group 檔案中,否則指令會執行失敗。

一、指令格式

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

二、指令功能

chgrp 可以透過群組名稱或群組識別碼(GID)來改變檔案或目錄的所屬群組,執行此操作通常需要超級使用者權限。

三、常用參數

必要參數:

-c 發生變更時輸出訊息
-f 不顯示錯誤訊息
-R 遞迴處理目錄及其子目錄下的所有檔案
-v 顯示詳細處理過程
--dereference 作用於符號連結的指向目標
--no-dereference 作用於符號連結本身

選擇參數:

--reference=<檔案或目錄> 以參照對象的群組為準
--help 顯示說明
--version 顯示版本資訊

四、實戰範例

範例一:變更單一檔案的群組

指令:

chgrp -v bin log2012.log

輸出:

[root@localhost test]# chgrp -v bin log2012.log
"log2012.log" 的所屬組已更改為 bin
[root@localhost test]# ll
---xrw-r-- 1 root bin 302108 11-13 06:03 log2012.log

說明:把 log2012.log 從 root 群組改為 bin 群組。

範例二:依參照檔案變更群組

指令:

chgrp --reference=log2012.log log2013.log

輸出:

[root@localhost test]# chgrp --reference=log2012.log log2013.log
[root@localhost test]# ll
---xrw-r-- 1 root bin 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log

說明:讓 log2013.log 的群組屬性與參照檔案 log2012.log 保持一致。

範例三:遞迴變更目錄下所有檔案的群組

指令:

chgrp -R bin test6

輸出:

[root@localhost test]# chgrp -R bin test6
[root@localhost test]# ll
drwxr-xr-x 2 root bin 4096 11-30 08:39 test6

說明:加上 -R 後,test6 目錄及其內部所有檔案的群組都會被調整為 bin。

範例四:以群組識別碼變更群組

指令:

chgrp -R 100 test6

輸出:

[root@localhost test]# chgrp -R 100 test6
[root@localhost test]# ll
drwxr-xr-x 2 root users 4096 11-30 08:39 test6

說明:100 是 users 群組的識別碼,可前往 /etc/group 檔案查詢各群組對應的 GID。

Copyright © Linux教程網 All Rights Reserved