zoukankan      html  css  js  c++  java
  • linux命令学习之:chown

      chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID;组可以是组名或者组ID;文件是以空格分开的要改变权限的文件列表,支持通配符。系统管理员经常使用chown命令,在将文件拷贝到另一个用户的名录下之后,让用户拥有使用该文件的权限。 

      只有文件主和超级用户才可以便用该命令。

    语法

      chown(选项)(参数)

    选项

    -c或——changes:效果类似“-v”参数,但仅回报更改的部分;

    -f或--quite或——silent:不显示错误信息;

    -h或--no-dereference:只对符号连接的文件作修改,而不更改其他任何相关文件;

    -R或——recursive:递归处理,将指定目录下的所有文件及子目录一并处理;

    -v或——version:显示指令执行过程;

    --dereference:效果和“-h”参数相同;

    --help:在线帮助;

    --reference=<参考文件或目录>:把指定文件或目录的拥有者与所属群组全部设成和参考文件或目录的拥有者与所属群组相同;

    --version:显示版本信息。

    参数

    用户:组:指定所有者和所属工作组。当省略“:组”,仅改变文件所有者;

    文件:指定要改变所有者和工作组的文件列表。支持多个文件和目标,支持shell通配符。

    常用示例

    1、新增用户以及用户组

    [root@CTU1000094641 ~]# groupadd test    #新增用户组test          
    [root@CTU1000094641 ~]# useradd -m -g test temptest  #新增用户组test下的用户temptest
    [root@CTU1000094641 ~]# passwd temptest    #给用户temptest设置密码,然后就可以使用了
    Changing password for user temptest.
    New password: 
    Retype new password: 
    passwd: all authentication tokens updated successfully.

    2.修改文件的用户以及用户组

      命令:chown  用户:用户组  文件   

    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 root      root 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
    [root@CTU1000094641 test]# chown temptest:test test1.xlsx 
    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx

      如果用户是唯一的标识,那么也可以用:chown  用户:  文件

    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
    [root@CTU1000094641 test]# chown temptest: test3.xlsx
    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 temptest  test 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx

    3、修改文件的用户为当前用户组下的用户(即使当前用户组没有该用户) 

      命令:chown  用户  文件    

    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 root      root 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
    [root@CTU1000094641 test]# chown temptest test1.xlsx      #事实上root用户组并没有temptest用户
    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest  root 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx

    4、修改文件所属的用户组

    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 temptest  test 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 temptest  test 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
    [root@CTU1000094641 test]# chown :test test4.xlsx
    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 temptest  test 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 temptest  test 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 root      test 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx

    5、修改目录及其下面的所有文件、子目录的文件主

      命令:chown -R  -v  拥有者:群组 文件目录(除了-R外与单文件修改没啥不同)

    [root@CTU1000094641 attachment]# chown -R -v temptest:test ./test
    changed ownership of `./test/test4.xlsx' to temptest:test
    ownership of `./test/test1.xlsx' retained as temptest:test
    ownership of `./test/test2.xlsx' retained as temptest:test
    changed ownership of `./test/testsub/test5.xlsx' to temptest:test
    changed ownership of `./test/testsub' to temptest:test
    ownership of `./test/test3.xlsx' retained as temptest:test
    changed ownership of `./test' to temptest:test
    [root@CTU1000094641 attachment]# cd test
    [root@CTU1000094641 test]# ll -lR
    .:
    total 136
    -rw-r--r--. 1 temptest test 26323 Dec  1 15:18 test1.xlsx
    -rw-r--r--. 1 temptest test 41035 Dec  1 15:18 test2.xlsx
    -rw-r--r--. 1 temptest test 22974 Dec  1 15:18 test3.xlsx
    -rw-r--r--. 1 temptest test 33698 Dec  1 15:18 test4.xlsx
    drwxr-xr-x. 2 temptest test  4096 Dec  1 15:20 testsub
    
    ./testsub:
    total 28
    -rw-r--r--. 1 temptest test 26323 Dec  1 15:20 test5.xlsx
  • 相关阅读:
    JavaScript操作符instanceof揭秘
    Linux打开txt文件乱码的解决方法
    Working copy locked run svn cleanup not work
    poj 2299 UltraQuickSort 归并排序求解逆序对
    poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
    poj2352 stars 树状数组
    poj 2286 The Rotation Game 迭代加深
    hdu 1800 Flying to the Mars
    poj 3038 Children of the Candy Corn bfs dfs
    hdu 1983 Kaitou Kid The Phantom Thief (2) DFS + BFS
  • 原文地址:https://www.cnblogs.com/jing99/p/7943130.html
Copyright © 2011-2022 走看看