Linux基础命令[29]-chown

2024-06-16 00:12
文章标签 基础 linux 命令 29 chown

本文主要是介绍Linux基础命令[29]-chown,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 1. chown 命令说明
    • 2. chown 命令语法
    • 3. chown 命令示例
      • 3.1 修改属主
      • 3.2 修改属组
      • 3.3 修改属主和属组
      • 3.4 修改文件夹所属
    • 4. 总结

1. chown 命令说明

chown:更改文件的用户或用户组,需要 root 用户或 sudo 权限的用户执行该命令。基本信息如下:

Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...or:  chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.-c, --changes          like verbose but report only when a change is made-f, --silent, --quiet  suppress most error messages-v, --verbose          output a diagnostic for every file processed--dereference      affect the referent of each symbolic link (this isthe 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 theownership of a symlink)--from=CURRENT_OWNER:CURRENT_GROUPchange the owner and/or group of each file only ifits current owner and/or group match those specifiedhere.  Either may be omitted, in which case a matchis 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 thanspecifying OWNER:GROUP values-R, --recursive        operate on files and directories recursivelyThe 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.-H                     if a command line argument is a symbolic linkto a directory, traverse it-L                     traverse every symbolic link to a directoryencountered-P                     do not traverse any symbolic links (default)--help     display this help and exit--version  output version information and exitOwner is unchanged if missing.  Group is unchanged if missing, but changed
to login group if implied by a ':' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.Examples:chown root /u        Change the owner of /u to "root".chown root:staff /u  Likewise, but also change its group to "staff".chown -hR root /u    Change the owner of /u and subfiles to "root".GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'chown invocation'

参数如下:

选项作用
-c只有真正更改时才会显示信息
-f忽略错误信息
-h修复符号链接
-v显示详细的处理信息
-R处理文件夹及文件夹中所有文件、子文件夹
-H配合 -R ,如果命令行参数是指向目录的符号链接,则遍历它
-L配合 -R ,遍历遇到的每个指向目录的符号链接
-P配合 -R ,不遍历任何符号链接(默认)

2. chown 命令语法

chown [OPTION]... [OWNER][:[GROUP]] FILE

3. chown 命令示例

3.1 修改属主

[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 root root  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root root 30 Jun  3 12:44 bbb
[root@localhost aaa]# chown test1 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 root  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root 30 Jun  3 12:44 bbb
[root@localhost aaa]# 

在这里插入图片描述

3.2 修改属组

修改属组需要加冒号

[root@localhost aaa]# chown :test1 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test1  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# id test2
uid=1015(test2) gid=1015(test2) groups=1015(test2)
[root@localhost aaa]# chown 1015 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test2 test1  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# 

在这里插入图片描述

3.3 修改属主和属组

[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test2 test1  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# chown test1:test2 a.txt 
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# 

在这里插入图片描述

3.4 修改文件夹所属

修改文件夹时,若不加 -R 选项,会只修改该文件夹的所属,加 -R 会将该文件夹下所有内容一起修改。

[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2  0 May 17 08:52 a.txt
drwxrwxrwx. 3 root  root  30 Jun  3 12:44 bbb
[root@localhost aaa]# chown test1:test1 bbb/
[root@localhost aaa]# ll
total 0
-r--rw-r--. 1 test1 test2  0 May 17 08:52 a.txt
drwxrwxrwx. 3 test1 test1 30 Jun  3 12:44 bbb
[root@localhost aaa]# cd bbb/
[root@localhost bbb]# ll
total 0
-rwxrwxrwx. 1 root root 0 Jun  3 12:44 1.txt
drwxrwxrwx. 2 root root 6 Jun  3 12:38 ccc
[root@localhost bbb]# cd ..
[root@localhost aaa]# chown -R test1:test1 bbb/
[root@localhost aaa]# cd bbb/
[root@localhost bbb]# ll
total 0
-rwxrwxrwx. 1 test1 test1 0 Jun  3 12:44 1.txt
drwxrwxrwx. 2 test1 test1 6 Jun  3 12:38 ccc
[root@localhost bbb]# 

在这里插入图片描述

4. 总结

chown:更改文件的用户或用户组,常用命令

chown 属主:属组 文件

若是想同步改变文件夹的内容所属,则加上 -R。

这篇关于Linux基础命令[29]-chown的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064977

相关文章

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

Linux之软件包管理器yum详解

《Linux之软件包管理器yum详解》文章介绍了现代类Unix操作系统中软件包管理和包存储库的工作原理,以及如何使用包管理器如yum来安装、更新和卸载软件,文章还介绍了如何配置yum源,更新系统软件包... 目录软件包yumyum语法yum常用命令yum源配置文件介绍更新yum源查看已经安装软件的方法总结软

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

Linux:alias如何设置永久生效

《Linux:alias如何设置永久生效》在Linux中设置别名永久生效的步骤包括:在/root/.bashrc文件中配置别名,保存并退出,然后使用source命令(或点命令)使配置立即生效,这样,别... 目录linux:alias设置永久生效步骤保存退出后功能总结Linux:alias设置永久生效步骤

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

关于Maven生命周期相关命令演示

《关于Maven生命周期相关命令演示》Maven的生命周期分为Clean、Default和Site三个主要阶段,每个阶段包含多个关键步骤,如清理、编译、测试、打包等,通过执行相应的Maven命令,可以... 目录1. Maven 生命周期概述1.1 Clean Lifecycle1.2 Default Li