One Linux command per day (chown) command

Source: Internet
Author: User

The Chown command alters the owner of a file or directory and the group it belongs to, which can authorize a user to become the owner of the specified file or to change the group to which the file belongs.

(1) Usage:

usage: chown [Options] ... Owner [: [Group]] file ...

or chown [options] ...--reference= reference file ...

(2) Function:

function: Change the owner and/or group of each file.

When you use the--REFEREBCE parameter, change the owner of the file and the owning group to the same as the specified reference file.

The user can be a user or user D, and the user group can be either a group name or a group ID. File names can have a list of files separated by spaces, and wildcard characters can be included in the file name.

(3) Option parameters:

1)-R--recursive recursive processing, processing all files and subdirectories under the specified directory

2)-F Ignore error message

3)-V displays detailed processing information

4)-C displays information about the changed section

5)--reference=< reference file or directory > The owner of the specified file or directory and the owning group are all set up and the owner of the reference file or directory is the same as the owning group

(4) Example:

1) [[email protected] documents]# chown-v Sunjimeng Finddir/dir/{head_text,less1,less2} Change the owner of the file (can also be separated by a space when naming multiple files)

[Email protected] documents]# ls-l finddir/dir Total dosage A-r-xr-xr-x.1Root root664May9  -: -Head_text-r-xr-xr-x.1Root root $May9  ,: theLess1-r-xr-xr-x.1Root root $May9  ,: -Less2[email protected] documents]# chown-V Sunjimeng finddir/dir/{HEAD_TEXT,LESS1,LESS2} When specifying a path, Finddir is under the current directory documents, so it cannot be taken with/. Changed ownership of"Finddir/dir/head_text"  fromROOT to sunjimengchanged ownership of"Finddir/dir/less1"  fromROOT to sunjimengchanged ownership of"Finddir/dir/less2"  fromroot to Sunjimeng[[email protected] documents]# ls-L finddir/dir Total dosage A-r-xr-xr-x.1Sunjimeng Root664May9  -: -Head_text-r-xr-xr-x.1Sunjimeng Root $May9  ,: theLess1-r-xr-xr-x.1Sunjimeng Root $May9  ,: -Less2

2) [[email protected] documents]# CHOWN-VR Sunjimeng finddir/dir recursively change the owning group of all files under the folder

[[email protected] documents]# ls-L finddir/dir Total dosage A-r-xr-xr-x.1Sunjimeng Root664May9  -: -Head_text-r-xr-xr-x.1Sunjimeng Root $May9  ,: theLess1-r-xr-xr-x.1Sunjimeng Root $May9  ,: -less2[[email protected] documents]# ls-l finddir Total dosage0Dr-xr-xr-x.2Root root $May +  .: -Dir//Does not change the owner of the folder in instance 1[email protected] documents]# chown-V Root finddir/dir/{HEAD_TEXT,LESS1,LESS2}//First restore owner to rootchanged ownership of"Finddir/dir/head_text"  fromSunjimeng to rootchanged ownership of"Finddir/dir/less1"  fromSunjimeng to rootchanged ownership of"Finddir/dir/less2"  fromSunjimeng to Root[[email protected] documents]# chown-VR Sunjimeng finddir/dir//re-recursively change the Dir folder and the file owner under it changed ownership of"Finddir/dir/head_text"  fromROOT to sunjimengchanged ownership of"Finddir/dir/less2"  fromROOT to sunjimengchanged ownership of"Finddir/dir/less1"  fromROOT to sunjimengchanged ownership of"Finddir/dir"  fromroot to Sunjimeng[[email protected] documents]# ls-l Finddir//See here, folder Dir and the owner of the file below have been changed to Sunjimeng total usage0Dr-xr-xr-x.2Sunjimeng Root $May +  .: -dir[[email protected] documents]# ls-L finddir/dir Total dosage A-r-xr-xr-x.1Sunjimeng Root664May9  -: -Head_text-r-xr-xr-x.1Sunjimeng Root $May9  ,: theLess1-r-xr-xr-x.1Sunjimeng Root $May9  ,: -Less2

3[[email protected] documents]# chown-vr Root:sunjimeng finddir change owner while changing owning group

[Email protected]ocalhost documents]# chown-VR Root:sunjimeng finddirchanged ownership of"Finddir/dir/head_text"  fromSunjimeng:root to root:sunjimengchanged ownership of"Finddir/dir/less2"  fromSunjimeng:root to root:sunjimengchanged ownership of"Finddir/dir/less1"  fromSunjimeng:root to root:sunjimengchanged ownership of"Finddir/dir"  fromSunjimeng:root to root:sunjimengchanged ownership of"Finddir"  fromroot:root to Root:sunjimeng[[email protected] documents]# ll finddir total dosage0Dr-xr-xr-x.2Root Sunjimeng $May +  .: -Dir[[email protected] documents]# ll Finddir/dir Total dosage A-r-xr-xr-x.1Root Sunjimeng664May9  -: -Head_text-r-xr-xr-x.1Root Sunjimeng $May9  ,: theLess1-r-xr-xr-x.1Root Sunjimeng $May9  ,: -Less2

4) [[email protected] dir]# chown-c. Root Head_text less1 only changes the owning group of the file (the-c parameter is similar to-V)

[[email protected] dir]# ll total dosage A-r-xr-xr-x.1Root Sunjimeng664May9  -: -Head_text-r-xr-xr-x.1Root Sunjimeng $May9  ,: theLess1-r-xr-xr-x.1Root Sunjimeng $May9  ,: -Less2[email protected] dir]# chown-c. Root Head_text less1changed ownership of"Head_text"  fromRoot:sunjimeng to:rootchanged Ownership of"Less1"  fromRoot:sunjimeng to:root[[email protected] dir]# chgrp-v Root head_text less2"Head_text"The owning group has been retained as root//Because the owning group of the file is already root, so this shows the reserved rootchanged Group of"Less2"  fromSunjimeng to Root[[email protected] dir]# ll total dosage A-r-xr-xr-x.1Root root664May9  -: -Head_text-r-xr-xr-x.1Root root $May9  ,: theLess1-r-xr-xr-x.1Root root $May9  ,: -Less2

5) [[email protected] dir]# chown-v: Sunjimeng less1 and [[email protected] dir]# chown-v: Sunjimeng less1 and [email protect Ed] dir]# chown-v Sunjimeng head_text

[[email protected] dir]# ll total dosage A-r-xr-xr-x.1Root root664May9  -: -Head_text-r-xr-xr-x.1Root root $May9  ,: theLess1-r-xr-xr-x.1Root root $May9  ,: -Less2[[email protected] dir]# chown-V:sunjimeng less1changed Ownership of"Less1"  fromroot:root to:sunjimeng[[email protected] dir]# chown-v. Sunjimeng less2changed ownership of"Less2"  fromroot:root to:sunjimeng[[email protected] dir]# chown-v Sunjimeng head_textchanged ownership of"Head_text"  fromroot to Sunjimeng[[email protected] dir]# ll total dosage A-r-xr-xr-x.1Sunjimeng Root664May9  -: -Head_text-r-xr-xr-x.1Root Sunjimeng $May9  ,: theLess1-r-xr-xr-x.1Root Sunjimeng $May9  ,: -Less2

(5) Other:

You can use the user name and user identification number settings when you change the owner or group of the file. Ordinary users cannot change their files to other owners. Its operation permissions are generally administrators.

The following options are used when the-r option is specified to set how to traverse the directory architecture. If you specify more than one option, only the last one will take effect.

-H if a command-line argument is a symbolic link to a directory, traversing the symbolic link-l iterates through each of the symbolic links that are encountered to the directory

-P do not traverse any symbolic links (default)

--HELP Display this help message and exit

--version display version information and exit if no owner is specified, it will not change.

The owning group will not change if it is not specified, but when ":" is added, group will change to the primary group of the specified owner. The owner and the owning group can be numbers or names.

One Linux command per day (chown) command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.