Linux系统中修改文件归属者和用户组

Linux系统中修改文件归属者和用户组

场景描述:

小开:亮哥,我使用root用户创建了一个文件,怎么修改文件归属者和归属用户组给另外一个用户呢?通过其他用户移动或者拷贝过来的文件怎么改变一下归属者和归属用户组呢?
亮哥:你查查chown命令,那个命令是修改文件owner和group的
小开:chown?你记错了没?不是chmod?
亮哥:当然不是了,chmod是给 文件赋权限,修改文件的归属者和用户组用这个。
小开:具体命令呢?提供一下
亮哥:chown 用户名 文件名
小开:好嘞,谢谢亮哥

那按照大牛的操作自己试一下吧

[root@localhost ~]# ll
-rw-r--r--. 1 root root    0 May 14 00:17 aaa
[root@localhost ~]# 
[root@localhost ~]# chown activemq aaa
[root@localhost ~]# ll
-rw-r--r--. 1 activemq root    0 May 14 00:17 aaa
[root@localhost ~]# 

小开:哇塞成功了耶,虽然用户权限成功了,但是组呢?还没有成功呀,继续研究吧

[root@localhost ~]# chown activemq.activemq aaa
[root@localhost ~]# ll
-rw-r--r--. 1 activemq activemq    0 May 14 00:17 aaa
[root@localhost ~]# 

小开:哎呦不错哦都成功了呀,据说还有其他方式再试一下另外命令


[root@localhost ~]# chown activemq:activemq aaa
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 activemq activemq    0 May 14 00:17 aaa
[root@localhost ~]# 

小开:也没有报错哦,是不是也可以呀,测试一下


[root@localhost ~]# chown root.root aaa
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root    0 May 14 00:17 aaa
[root@localhost ~]# chown activemq:activemq aaa
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 activemq activemq    0 May 14 00:17 aaa
[root@localhost ~]# 

居然真可以,那赋值文件夹呢?是不是也是这样呢?重新来一次

[root@localhost ~]# ll
drwxr-xr-x. 5 root     root       61 May 14 00:15 test
[root@localhost ~]# chown activemq:activemq test/
drwxr-xr-x. 5 activemq activemq   61 May 14 00:15 test
[root@localhost ~]# cd test/
[root@localhost test]# ll
total 0
drwxr-xr-x. 2 root root 6 May 14 00:14 dir
drwxr-xr-x. 2 root root 6 May 14 00:14 dir2
-rw-r--r--. 1 root root 0 May 14 00:15 file
-rw-r--r--. 1 root root 0 May 14 00:15 file2
drwxr-xr-x. 2 root root 6 May 14 00:14 this
[root@localhost test]# 

小开:亮哥,那个命令可以用,成功是成功了,但文件夹里面的内容可没有成功哦,
那怎么办呢?
亮哥:我现在有点忙,你先看看,
小开:好的亮哥。找呀找,找不到

咦想起来了,有个命令 男人一下吗 ,哈哈对对对 男人一下

亮哥:对,manual 一下,这才是学习进步的节奏,学习要考主动,别人能帮你一阵子帮不了你一辈子,学习还是要靠自己的。

于是乎,小开就开启了研究之路

man chown 

详细描述

描述:chown 修改每个由第一个非选项参数声明的给定 file(文件) 的用户和/或组的所有权.如下: 如果只给出了用户名(或者数字用户标识),那么该用户即成为每个指定文件的所有者,而该文件的组别并不改变.如果用户名后面紧跟着冒号和 组名(或者是数字组标识),并且它们之间没有空格,那么文件的组所有权 也随之改变.
       -c, --changes 
         like verbose but report only when a change is made
         详尽地描述每个 file 实际改变了哪些所有权.
       -f, --silent, --quiet
         suppress most error messages
         不打印文件所有权不能修改的报错信息.
       -v, --verbose
         output a diagnostic for every file processed
         详尽地描述对每个 file 所执行的操作(或者无操作)
       --dereference
         affect the referent of each symbolic link (this is the default), rather than the symbolic link itself
         修改符号链接目标端的所有权,而非符号链接自身.
       -h, --no-dereference
         affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)
         只作用于其本身的符号链接,而不修改它们所指向的文件.  这只在提供了 lchown 系统调用的情况下才可用.
       --from=CURRENT_OWNER:CURRENT_GROUP
         change the owner and/or group of each file only if its current owner and/or group match those specified here.  Either may be omitted, in which case a match is not required for the omitted attribute
         仅当每个文件的当前所有者和/或组与此处指定的所有者和/或组匹配时,才更改其所有者和/或组。在哪种情况下,省略的属性不需要匹配
       --no-preserve-root
         do not treat '/' specially (the default)
         不要特别处理“/”(默认)
       --preserve-root
         fail to operate recursively on '/'
         无法在“/”上递归操作
       --reference=RFILE
         use RFILE,s owner and group rather than specifying OWNER:GROUP values
         使用文件的所有者和组,而不是指定所有OWNER:GROUP值
       -R, --recursive
         operate on files and directories recursively The  following options modify how a hierarchy is traversed when the -R option is also specified.  If more than one is specified, only the final
       one takes effect.
         递归操作文件和目录以下选项修改在同时指定-r选项时如何遍历层次结构。如果指定了不止一个,则只有最后一个一个生效。
       -H  
         if a command line argument is a symbolic link to a directory, traverse it
         如果命令行参数是指向目录的符号链接,则遍历它
       -L   
         traverse every symbolic link to a directory encountered
         遍历遇到的目录的每个符号链接
       -P   
         do not traverse any symbolic links (default)
         对所有连接不生效(默认)
       --help 
	   display this help and exit
         在标准输出上打印一条用法信息,并以成功状态退出.
       --version 
	    output version information and exit
         在标准输出上打印版本信息,然后以成功状态退出.

继续操作

[root@localhost ~]# chown activemq:activemq -R test/
[root@localhost ~]# ll
drwxr-xr-x. 5 activemq activemq   61 May 14 00:15 test
[root@localhost ~]# cd test/
[root@localhost test]# ll
drwxr-xr-x. 2 activemq activemq 6 May 14 00:14 dir
drwxr-xr-x. 2 activemq activemq 6 May 14 00:14 dir2
-rwxr-xr-x. 1 activemq activemq 0 May 14 00:15 file
-rw-r--r--. 1 activemq activemq 0 May 14 00:15 file2
drwxr-xr-x. 2 activemq activemq 6 May 14 00:14 this

终于成功了,还有一些命令参数,请大家自己动手操作一下吧

参考官网的翻译

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值