chown命令改变某个文件或目录的所有者和所属的组,该命令可以向某个用户授权,使该用户变成指定文件的所有者或者改变文件所属的组。

    (1)用法:

    用法:  chown [选项]... [所有者][:[组]] 文件...

或   chown [选项]... --reference=参考文件 文件...

    (2)功能:

功能:  更改每个文件的所有者和/或所属组。

当使用 --referebce 参数时,将文件的所有者和所属组更改为与指定参考文件相同。

用户可以是用户或者是用户D,用户组可以是组名或组id。文件名可以使由空格分开的文件列表,在文件名中可以包含通配符。

    (3)选项参数:

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

2) -f                   忽略错误信息

3) -v                    显示详细的处理信息

4) -c                    显示更改的部分的信息

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

    (4)实例:

1)[root@localhost Documents]# chown -v sunjimeng findDir/Dir/{head_text,less1,less2}        改变文件的拥有者(指名多个文件时还可以用空格隔开)

[root@localhost Documents]# ls -l findDir/Dir
总用量
-r-xr-xr-x. root root 5月 : head_text
-r-xr-xr-x. root root 5月 : less1
-r-xr-xr-x. root root 5月 : less2
[root@localhost Documents]# chown -v sunjimeng findDir/Dir/{head_text,less1,less2} 在指明路径时,findDir在当前目录Documents下,所以不能带/。
changed ownership of "findDir/Dir/head_text" from root to sunjimeng
changed ownership of "findDir/Dir/less1" from root to sunjimeng
changed ownership of "findDir/Dir/less2" from root to sunjimeng
[root@localhost Documents]# ls -l findDir/Dir
总用量
-r-xr-xr-x. sunjimeng root 5月 : head_text
-r-xr-xr-x. sunjimeng root 5月 : less1
-r-xr-xr-x. sunjimeng root 5月 : less2

2)[root@localhost Documents]# chown -vR sunjimeng findDir/Dir          递归地将文件夹下的所有文件的所属组改变

[root@localhost Documents]# ls -l findDir/Dir
总用量
-r-xr-xr-x. sunjimeng root 5月 : head_text
-r-xr-xr-x. sunjimeng root 5月 : less1
-r-xr-xr-x. sunjimeng root 5月 : less2
[root@localhost Documents]# ls -l findDir
总用量
dr-xr-xr-x. root root 5月 : Dir //在实例1中并没有改变文件夹的所有者
[root@localhost Documents]# chown -v root findDir/Dir/{head_text,less1,less2} //先把所有者复原为root
changed ownership of "findDir/Dir/head_text" from sunjimeng to root
changed ownership of "findDir/Dir/less1" from sunjimeng to root
changed ownership of "findDir/Dir/less2" from sunjimeng to root
[root@localhost Documents]# chown -vR sunjimeng findDir/Dir //再递归地把Dir文件夹和其下的文件所有者更改
changed ownership of "findDir/Dir/head_text" from root to sunjimeng
changed ownership of "findDir/Dir/less2" from root to sunjimeng
changed ownership of "findDir/Dir/less1" from root to sunjimeng
changed ownership of "findDir/Dir" from root to sunjimeng
[root@localhost Documents]# ls -l findDir              //在这里查看,文件夹Dir及其下文件的所有者都已经改变为sunjimeng
总用量
dr-xr-xr-x. sunjimeng root 5月 : Dir
[root@localhost Documents]# ls -l findDir/Dir
总用量
-r-xr-xr-x. sunjimeng root 5月 : head_text
-r-xr-xr-x. sunjimeng root 5月 : less1
-r-xr-xr-x. sunjimeng root 5月 : less2

3[root@localhost Documents]# chown -vR root:sunjimeng findDir              改变所有者的同时改变所属组

[root@localhost Documents]# chown -vR root:sunjimeng findDir
changed ownership of "findDir/Dir/head_text" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir/less2" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir/less1" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir/Dir" from sunjimeng:root to root:sunjimeng
changed ownership of "findDir" from root:root to root:sunjimeng
[root@localhost Documents]# ll findDir
总用量
dr-xr-xr-x. root sunjimeng 5月 : Dir
[root@localhost Documents]# ll findDir/Dir
总用量
-r-xr-xr-x. root sunjimeng 5月 : head_text
-r-xr-xr-x. root sunjimeng 5月 : less1
-r-xr-xr-x. root sunjimeng 5月 : less2

4)[root@localhost Dir]# chown -c .root head_text less1                 只改变文件的所属组(-c参数与-v类似)

[root@localhost Dir]# ll
总用量
-r-xr-xr-x. root sunjimeng 5月 : head_text
-r-xr-xr-x. root sunjimeng 5月 : less1
-r-xr-xr-x. root sunjimeng 5月 : less2
[root@localhost Dir]# chown -c .root head_text less1
changed ownership of "head_text" from root:sunjimeng to :root
changed ownership of "less1" from root:sunjimeng to :root
[root@localhost Dir]# chgrp -v root head_text less2
"head_text" 的所属组已保留为root //因为文件的所属组已经为root了,所以这里显示保留为root
changed group of "less2" from sunjimeng to root
[root@localhost Dir]# ll
总用量
-r-xr-xr-x. root root 5月 : head_text
-r-xr-xr-x. root root 5月 : less1
-r-xr-xr-x. root root 5月 : less2

5)[root@localhost Dir]# chown -v :sunjimeng less1  和[root@localhost Dir]# chown -v :sunjimeng less1 和[root@localhost Dir]# chown -v sunjimeng head_text

[root@localhost Dir]# ll
总用量
-r-xr-xr-x. root root 5月 : head_text
-r-xr-xr-x. root root 5月 : less1
-r-xr-xr-x. root root 5月 : less2
[root@localhost Dir]# chown -v :sunjimeng less1
changed ownership of "less1" from root:root to :sunjimeng
[root@localhost Dir]# chown -v .sunjimeng less2
changed ownership of "less2" from root:root to :sunjimeng
[root@localhost Dir]# chown -v sunjimeng head_text
changed ownership of "head_text" from root to sunjimeng
[root@localhost Dir]# ll
总用量
-r-xr-xr-x. sunjimeng root 5月 : head_text
-r-xr-xr-x. root sunjimeng 5月 : less1
-r-xr-xr-x. root sunjimeng 5月 : less2

    (5)其他:

在更改文件的所有者或所属群组时,可以使用用户名称和用户识别码设置。普通用户不能将自己的文件改变成其他的拥有者。其操作权限一般为管理员。

以下选项是在指定了 -R 选项时被用于设置如何穿越目录结构体系。 如果您指定了多于一个选项,那么只有最后一个会生效。

  -H 如果命令行参数是一个通到目录的符号链接,则遍历符号链接 -L 遍历每一个遇到的通到目录的符号链接

  -P 不遍历任何符号链接(默认)

  --help 显示此帮助信息并退出

  --version 显示版本信息并退出 如果没有指定所有者,则不会更改。

  所属组若没有指定也不会更改,但当加上 ":"时 GROUP 会更改为指定所有者的主要组。所有者和所属组可以是数字或名称。

每天一个Linux命令(26)chown命令的更多相关文章

  1. 详解Linux chgrp和chown命令的用法

    Linux chgrp和chown命令是管理员的常用命令,对于初学Linux系统管理的人来说,这对Linux chgrp和chown命令具体的用法这里做一介绍. Linux chgrp命令 功能:改变 ...

  2. 每天一个linux命令31)--chown命令

    chown将 指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID,组可以使组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷 ...

  3. linux命令之chown命令

    发布:JB01   来源:脚本学堂     [大 中 小] 本文介绍下,linux系统中用于文件与目录权限管理的命令 chown命令的用法,chown将指定文件的拥有者改为指定的用户或组.有需要的朋友 ...

  4. linux的chmod,chown命令详解

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  5. linux的chmod,chown命令 详解

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  6. linux常用命令:chown 命令

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...

  7. linux命令之------Chown命令

    Chown命令 1) 作用:将指定文件的拥有者改为指定的用户或组. 2) -c:显示更改的部分的信息. 3)-f:忽略错误信息. 4)-h:修复符号链接. 5)-v:显示详细的处理信息. 6)-R:处 ...

  8. linux chmod命令和chown命令

    一.chmod及文件权限 1.了解文件权限 root账户新建一个目录permission,在该目录新建一个文件file,通过ll就可以查看其权限. root@development:~# cd per ...

  9. 【转载】每天一个Linux命令

    目  录 每天一个linux命令(1)  : ls 命令 每天一个linux命令(2)  : cd 命令 每天一个linux命令(3)  : pwd 命令 每天一个linux命令(4)  : mkdi ...

  10. Linux自学之旅-基础命令(chown和chgrp)

    转: Linux自学之旅-基础命令(chown和chgrp) Linux自学之旅-基础命令(改变所有者与所属组的命令) 文章目录 前言 一.chown命令 二.chgrp命令 总结 前言 1.上一节我 ...

随机推荐

  1. angular中通过CSS使下拉列表默认值变灰

    angular版本:angular5 先看效果图: drop down的样式是我用CSS样式控制的,没有用插件.想要改变Drop Down List里的默认值的颜色,我的思路是这样的. 在<se ...

  2. lua table库

      整理自:http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543139.html 1.table.concat(table, sep,  st ...

  3. Sphinx初探之安装

    在Centos or redhat 安装Sphinx .首先安装依赖包 $ yum install postgresql-libs unixODBC .安装软件 $ rpm -Uhv sphinx-- ...

  4. Oracle的substr函数简单用法与substring区别

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  5. PHP将多级目录打包成zip文件

    最近接触PHP,需要用到zip压缩,在网上搜索的一大堆,发现代码都不低于50行.  而且调用还很费事(基础太少看不懂).让我收获的是Php提供有一个ZipArchive类,并有如下方法. bool a ...

  6. Java反射学习系列-绪论

    Java反射学习系列-绪论 https://blog.csdn.net/hanchao5272/article/details/79358924

  7. java后端技术

    技术概论:Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafKa j2ee分布式架构 我在恒生工作,主要开发金融互联网第三方平台的对接项目.目前已经对接 ...

  8. 解决Mysql - can't get stat of (errcode:13)

    [1]线上环境问题 大清早来上班,被运维扔了一个错误日志截图,如下: Mysql 导入数据失败! [2]分析问题 记得如此类似的问题,好像已经都解决了.为啥又复现了呢?难道是自己上次没有考虑全场景?或 ...

  9. Android 适配(drawable文件夹)图片适配(二)

    参考自(https://blog.csdn.net/myoungmeng/article/details/54090891) Android资源文件存放: android的drawable文件一共可以 ...

  10. thinkphp自动验证无效的问题

    新手入门thinkphp,试用自动验证表单输入数据功能,却发现怎么都不能调用自动验证,自动验证无效,原因竟是一个小细节的疏忽,学习一定要细心啊! Action方法: IndexAction下的adds ...