zoukankan      html  css  js  c++  java
  • 每天一个Linux命令(25)chgrp命令

          chgrp命令用来改变文件或目录所属的用户组。

        (1)用法:

        用法:  chgrp  [选项参数] [组] [文件]

             或 chgrp  [选项]   组文件...   POSIX 选项: [-R] [--]

        (2)功能:

        功能:  改变文件的组所有权

        (3)选项参数:

        1) -c  --changes             效果类似“-v”参数,但仅回报更改的部分

        2) -f  --quiet  --silent           不显示错误信息

      3) -h  --no-dereference          只对符号连接的文件作修改,而不是该其他任何相关文件

      4) -R  --recursive             递归处理,将指令目录下的所有文件及子目录一并处理

        5) -v  --verbose             显示指令执行过程

        6) --reference=<参考文件或目录>           把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同

     

        (4)实例:

          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

          群组识别码:

    [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
  • 相关阅读:
    matlab如何写一个类
    最大稳定极值区域(MSER)检测
    Excel中公式的绝对引用和相对引用单元格
    c++中自增(++)和自减(--)操作符
    C++中的c_str()函数用法
    一些常用的图像数据库
    浅谈C++中指针和引用的区别
    selenium之find_element_by_xpath定位元素
    python selenium使用
    H5页面调用手机扫一扫功能
  • 原文地址:https://www.cnblogs.com/MenAngel/p/5516135.html
Copyright © 2011-2022 走看看