歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(25)chgrp命令

每天一個Linux命令(25)chgrp命令

日期:2017/3/3 12:29:37   编辑:Linux技術

[b] chgrp命令用來改變文件或目錄所屬的用戶組。[/b] [b] (1)用法:[/b]

[b] 用法: chgrp [選項參數] [組] [文件][/b] 或 chgrp [選項] 組文件... POSIX 選項: [-R] [--]

[b](2)功能:[/b][b] 功能: 改變文件的組所有權[/b]

[b] (3)選項參數:[/b] 1) -c --changes             效果類似“-v”參數,但僅回報更改的部分

2) -f --quiet --silent           不顯示錯誤信息  3) -h --no-dereference         只對符號連接的文件作修改,而不是該其他任何相關文件

  4) -R --recursive            遞歸處理,將指令目錄下的所有文件及子目錄一並處理 5) -v --verbose             顯示指令執行過程

6) --reference=<參考文件或目錄> 把指定文件或目錄的所屬群組全部設成和參考文件或目錄的所屬群組相同[b] (4)實例:[/b]

1)[root@localhost sunjimeng]# chgrp -v root Document          將Document所在組改為root

[root@localhost sunjimeng]# ll
總用量 0
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Desktop
drwxrwxr-x. 3 sunjimeng sunjimeng 100 5月  19 22:28 Document
drwxr-xr-x. 5 root      root       44 5月  21 21:52 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Downloads
drwxrwxr-x. 2 sunjimeng sunjimeng   6 5月  17 04:55 findTextDir
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Videos
[root@localhost sunjimeng]# chgrp -v root Document
changed group of "Document" from sunjimeng to root
[root@localhost sunjimeng]# ll
總用量 0
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Desktop
drwxrwxr-x. 3 sunjimeng root      100 5月  19 22:28 Document
drwxr-xr-x. 5 root      root       44 5月  21 21:52 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Downloads
drwxrwxr-x. 2 sunjimeng sunjimeng   6 5月  17 04:55 findTextDir
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng   6 5月   1 01:23 Videos

2)[root@localhost Document]# chgrp -v --reference=newDir all.txt            將文件所屬組設置為同某一個文件或文件夾一樣

[root@localhost Document]# ll
總用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
[root@localhost Document]# chgrp -v  --reference=newDir all.txt
"all.txt" 的所屬組已保留為root
[root@localhost Document]# ll
總用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt

3)[root@localhost sunjimeng]# chgrp -vR sunjimeng Document       改變指定目錄以及其子目錄下的所有文件的群組屬性

[root@localhost sunjimeng]# chgrp -vR sunjimeng Document
changed group of "Document/newDir/mvt1.txt" from root to sunjimeng
changed group of "Document/newDir/mvt2.txt" from root to sunjimeng
changed group of "Document/newDir/mvt3.txt" from root to sunjimeng
changed group of "Document/newDir" from root to sunjimeng
changed group of "Document/t1.txt" from root to sunjimeng
changed group of "Document/t2.txt" from root to sunjimeng
changed group of "Document/all.txt" from root to sunjimeng
"Document/B.text3" 的所屬組已保留為sunjimeng
"Document/C.text6" 的所屬組已保留為sunjimeng
"Document/D.text" 的所屬組已保留為sunjimeng
changed group of "Document" from root to sunjimeng
[root@localhost sunjimeng]# ls -l Document
總用量 12
-rw-r--r--. 1 root      sunjimeng 85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      sunjimeng 51 5月  18 02:47 newDir
-rw-r--r--. 1 root      sunjimeng 42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      sunjimeng 43 5月  18 02:54 t2.txt

4)[root@localhost sunjimeng]# chgrp -vR 100 Document           通過群組識別碼改變文件群組屬性,100為users群組的識別碼,具體群組和群組識別碼可以去/etc/group文件中查看

[root@localhost sunjimeng]# chgrp -vR 100 Document
changed group of "Document/newDir/mvt1.txt" from sunjimeng to 100
changed group of "Document/newDir/mvt2.txt" from sunjimeng to 100
changed group of "Document/newDir/mvt3.txt" from sunjimeng to 100
changed group of "Document/newDir" from sunjimeng to 100
changed group of "Document/t1.txt" from sunjimeng to 100
changed group of "Document/t2.txt" from sunjimeng to 100
changed group of "Document/all.txt" from sunjimeng to 100
changed group of "Document/B.text3" from sunjimeng to 100
changed group of "Document/C.text6" from sunjimeng to 100
changed group of "Document/D.text" from sunjimeng to 100
changed group of "Document" from sunjimeng to 100
[root@localhost sunjimeng]# ls -l Document
總用量 12
-rw-r--r--. 1 root      users 85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng users  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng users  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng users  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      users 51 5月  18 02:47 newDir
-rw-r--r--. 1 root      users 42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      users 43 5月  18 02:54 t2.txt
[b] 群組識別碼:[/b]

[root@localhost sunjimeng]# 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:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
utmp:x:22:
utempter:x:35:
systemd-journal:x:190:
dbus:x:81:
polkitd:x:999:
cgred:x:998:
tss:x:59:
colord:x:997:
usbmuxd:x:113:
dip:x:40:
ntp:x:38:
ssh_keys:x:996:
libstoragemgmt:x:995:
saslauth:x:76:
rpc:x:32:
rtkit:x:172:
chrony:x:994:
radvd:x:75:
rpcuser:x:29:
nfsnobody:x:65534:
kvm:x:36:qemu
qemu:x:107:
abrt:x:173:
sssd:x:993:
avahi-autoipd:x:170:
unbound:x:992:
pulse-access:x:991:
pulse:x:171:
gdm:x:42:
gnome-initial-setup:x:990:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
slocate:x:21:
avahi:x:70:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
sunjimeng:x:1000:
5)[sunjimeng@localhost Document]$ chgrp -vf sunjimeng findDir      -v是不顯示錯誤信息,v命令顯示執行的步驟    

[root@localhost Documents]# ll
總用量 0
dr--r--r--. 3 root root 16 5月  21 21:52 findDir
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
[root@localhost Documents]# exit
exit
[sunjimeng@localhost ~]$ cd Document
[sunjimeng@localhost Document]$ chgrp -v sunjimeng findDir
chgrp: 無法訪問"findDir": 沒有那個文件或目錄
無法更改"findDir" 的所屬組為sunjimeng
[sunjimeng@localhost Document]$ chgrp -vf sunjimeng findDir
無法更改"findDir" 的所屬組為sunjimeng

Copyright © Linux教程網 All Rights Reserved