linux系统下chown命令介绍及案例使用

韵味老鸟 2024-09-03 13:53:09

linux 系统下 chown 命令介绍及案例使用

Linux 系统下 chown 命令用于更改文件和目录的所有者和所属组

chown 命令介绍

基本语法:

bashchown [选项] 所有者[:组] 文件名所有者:指定新的文件所有者的用户名或 UID。组:指定新的文件所属组的组名或 GID。可选,如果不指定则保持不变。文件名:要改变所有权的文件或目录。

常用选项:

-R:递归地改变目录及其子目录下的所有文件。-c:当更改所有权时,显示每个处理的文件的信息。-f:抑制大部分错误信息。-v:详细显示每个处理的文件的信息。

命令:

root@meng:~# which chown

/usr/bin/chown

root@meng:~# chown -h

chown: missing operand

Try 'chown --help' for more information.

root@meng:~# chown --help

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 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)

--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

-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.

-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

Owner 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: <https://www.gnu.org/software/coreutils/>

Report any translation bugs to <https://translationproject.org/team/>

Full documentation <https://www.gnu.org/software/coreutils/chown>

or available locally via: info '(coreutils) chown invocation'

命令案例:

root@meng:~# ls -al meng.txt

-rwxr-xr--. 1 root meng 45 Aug 22 09:37 meng.txt

root@meng:~# chown meng meng.txt

root@meng:~# ls -al meng.txt

-rwxr-xr--. 1 meng meng 45 Aug 22 09:37 meng.txt

root@meng:~# chown -c meng meng.txt

root@meng:~# chown -v meng meng.txt

ownership of 'meng.txt' retained as meng

root@meng:~# ls -al meng.txt

-rwxr-xr--. 1 meng meng 45 Aug 22 09:37 meng.txt

root@meng:~# chown -v root.meng meng.txt

changed ownership of 'meng.txt' from meng:meng to root:meng

root@meng:~# ls -al meng.txt

-rwxr-xr--. 1 root meng 45 Aug 22 09:37 meng.txt

0 阅读:0

韵味老鸟

简介:感谢大家的关注