zoukankan      html  css  js  c++  java
  • Linux命令之chown

    chown [选项] … [OWNER][:[GROUP]] FILE …

    chown [选项] … --reference=RFILE FILE …

      chown更改每个给定文件的用户和/或组的所有权。如果仅给出所有者(用户名或UID),则该用户成为每个给定文件的所有者,并且不更改文件的组。如果所有者后跟冒号和组名(或GID),并且它们之间没有空格,则文件的组所有权也会更改。如果所有者后跟冒号但没有组名(或GID),则该用户将成为文件的所有者,并且文件的组将改为该用户的登录组。如果给出冒号和组名(或GID),但省略所有者,则只更改文件的组,这种情况下chown与chgrp执行相同功能。如果仅有冒号,或为空,则不更改所有者和组。

    (1).选项

    -c,--changes 类似verbose,但只有在更改时报告
    -f,--silent,--quiet 不列出大多数错误信息
    -v,--verbose 为每个处理的文件输出诊断信息
    --derference 影响每个符号链接的引用(这是默认值),而不是符号链接本身
    -h,--no-dereference 影响符号链接本身而不是任何引用的文件(仅在可以更改符号链接的所有权的系统上有用)
    --from=CURRENT_OWNER:CURRENT_GROUP 如果文件当前的所有者和/或组与CURRENT_OWNER和/或CURRENT_GROUP匹配时执行更改。两个都可以省略,省略的属性不需要匹配。
    --no-preserve-root 要特别对待’/’(根目录?)(默认)
    --preserve-root 无法以’/’(根目录?)递归操作
    --reference=RFILE 使用RFILE的所有者和组而不是指定OWNER:GROUP值
    --help 打印帮助信息并退出
    --version 打印版本信息并退出
    -R,--recursive 以递归方式操作文件和目录

    当-R选项被指定时,以下选项修改了如何遍历层次结构。如果指定了多个选项,只有最后一个生效。

    -H 如果一个命令行的参数是符号链接,遍历它
    -L 遍历目录里遇到的每一个符号链接
    -P 不要遍历任何符号链接(默认)

    (2).实例

     更改文件所有者

    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 root root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 root root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 root root 0 11月  9 15:41 3.txt
    [root@xuexi ~]# chown -v xf newDir/Dir/*    //*表示所有文件
    changed ownership of "newDir/Dir/1.txt" from root to xf
    changed ownership of "newDir/Dir/2.txt" from root to xf
    changed ownership of "newDir/Dir/3.txt" from root to xf
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 xf root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 xf root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 xf root 0 11月  9 15:41 3.txt
    [root@xuexi ~]# chown -v root newDir/Dir/{1.txt,2.txt}     //多个文件用逗号隔开
    changed ownership of "newDir/Dir/1.txt" from xf to root
    changed ownership of "newDir/Dir/2.txt" from xf to root
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 root root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 root root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 xf   root 0 11月  9 15:41 3.txt
    

    递归更改文件夹及其下文件的所有者

    [root@xuexi ~]# ls -l newDir/
    总用量 0
    drwxr-xr-x. 2 root root 45 11月  9 15:41 Dir
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 root root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 root root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 xf   root 0 11月  9 15:41 3.txt
    [root@xuexi ~]# chown -vR xf newDir/Dir
    changed ownership of "newDir/Dir/1.txt" from root to xf
    changed ownership of "newDir/Dir/2.txt" from root to xf
    "newDir/Dir/3.txt" 的所有者已保留为xf
    changed ownership of "newDir/Dir" from root to xf
    [root@xuexi ~]# ls -l newDir/
    总用量 0
    drwxr-xr-x. 2 xf root 45 11月  9 15:41 Dir
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 xf root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 xf root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 xf root 0 11月  9 15:41 3.txt
    

    同时更改文件的所有者和所属组

    [root@xuexi ~]# ls -l newDir/
    总用量 0
    drwxr-xr-x. 2 xf root 45 11月  9 15:41 Dir
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 xf root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 xf root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 xf root 0 11月  9 15:41 3.txt
    [root@xuexi ~]# chown -vR root:xf newDir/Dir
    changed ownership of "newDir/Dir/1.txt" from xf:root to root:xf
    changed ownership of "newDir/Dir/2.txt" from xf:root to root:xf
    changed ownership of "newDir/Dir/3.txt" from xf:root to root:xf
    changed ownership of "newDir/Dir" from xf:root to root:xf
    [root@xuexi ~]# ls -l newDir/
    总用量 0
    drwxr-xr-x. 2 root xf 45 11月  9 15:41 Dir
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 root xf 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 root xf 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 root xf 0 11月  9 15:41 3.txt
    

    只更改文件的所属组

    [root@xuexi ~]# ls -l newDir/
    总用量 0
    drwxr-xr-x. 2 root xf 45 11月  9 15:41 Dir
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 root xf 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 root xf 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 root xf 0 11月  9 15:41 3.txt
    [root@xuexi ~]# chown -vR :root newDir/Dir 
    changed ownership of "newDir/Dir/1.txt" from root:xf to :root
    changed ownership of "newDir/Dir/2.txt" from root:xf to :root
    changed ownership of "newDir/Dir/3.txt" from root:xf to :root
    changed ownership of "newDir/Dir" from root:xf to :root
    [root@xuexi ~]# ls -l newDir/
    总用量 0
    drwxr-xr-x. 2 root root 45 11月  9 15:41 Dir
    [root@xuexi ~]# ls -l newDir/Dir/
    总用量 0
    -rw-r--r--. 1 root root 0 11月  9 15:41 1.txt
    -rw-r--r--. 1 root root 0 11月  9 15:41 2.txt
    -rw-r--r--. 1 root root 0 11月  9 15:41 3.txt
    

    (3).扩展

    chgrp [GROUP] FILE

    一样可以修改所属组

  • 相关阅读:
    [LeetCode] Contains Duplicate 包含重复值
    [LeetCode] 281. The Skyline Problem 天际线问题
    Qt resizeEvent 控件居中设置
    [LeetCode] 214. Shortest Palindrome 最短回文串
    Qt Creator Shortcuts 快捷键大全
    Qt SizePolicy 属性
    [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串
    [LeetCode] 213. House Robber II 打家劫舍之二
    [LeetCode] 212. Word Search II 词语搜索之二
    [LeetCode] 18. 4Sum 四数之和
  • 原文地址:https://www.cnblogs.com/diantong/p/9935328.html
Copyright © 2011-2022 走看看