Comprehensive Guide to Linux Commands – chown Command

Click the blue text to follow us

Comprehensive Guide to Linux Commands - chown Command

1. Introduction

The chown (change owner) command is used to modify the owner and group of a file.

Linux/Unix is a multi-user, multitasking operating system, and all files have an owner. The chown command changes the owner of a specified file to a specified user or group. The user can be a username or user ID, and the group can be a group name or group ID. The files are specified as a space-separated list, and wildcards are supported.

Note: The chown command requires superuser (root) privileges to execute. Non-superusers may need to use the chgrp command to set the associated group (for details, refer to the Comprehensive Guide to Linux Commands – chgrp Command).

2. Syntax

Syntax: chown [options]… [owner][:[group]] file…

Parameter Description:

-R, –recursive: Recursively change the owner of all files in the directory and its subdirectories

-v, –verbose: Display detailed operation information

-c, –changes: Only show files that actually changed

-f, –silent, –quiet: Suppress error messages

–reference=file: Use the owner and group of the reference file

-h, –no-dereference: Affect the symbolic link itself, not the file it points to

–from=current owner: Change only if the current owner matches

3. Practical Examples

1. Change the owner of a single file or directory

[root@app01 abc]# chown tom 1.txt

2. Change both the owner and group of a single file or directory

[root@app01 abc]# chown tom:tom 1.txt

3. Change only the group of a single file or directory

[root@app01 abc]# chown :tom 2.zip

Note: When changing only the group of a file or directory, the colon (:) must be included; otherwise, the owner will be changed as well.

4. Change using user ID or group ID

[root@app01 abc]# chown 1002:1002 1.txt

Where 1002 represents the user ID and group ID respectively.

5. Change the owner and group of all files or subdirectories within a directory

[root@app01 abc]# chown -R tom:tom test

6. Change only matching conditions

[root@app01 abc]# chown --from=tom root 1.txt

The from parameter means that the change will only occur if the owner is tom, changing it to owner root.

7. Set based on a reference file

[root@app01 abc]# chown --reference=/app/abc/1.txt a.txt

This means that the owner and group of a.txt will be set according to the permissions of 1.txt.

8. Handling symbolic link files

// Change the symbolic link itself [root@app01 abc]# chown -h tom:tom /home/tom/linkfile // Change the file pointed to by the symbolic link [root@app01 abc]# chown tom:tom /home/tom/linkfile

Here, linkfile represents both the original file and the link file of the symbolic link.

The -h option will change the identification information of the symbolic link itself, and will not modify the owner of the target file or directory, even if the link points to a specific file or directory.

For example, if the symbolic link points to the file test.txt, executing chown -h user1:group1 linkfile will change the owner of linkfile to user1, but the owner of test.txt will not change.

This is mainly used when you need to control the permissions of the symbolic link separately, avoiding affecting the permission settings of the actual file or directory. In this case, the -h parameter option can be used to achieve this.

Long press the QR code to follow us

Comprehensive Guide to Linux Commands - chown CommandComprehensive Guide to Linux Commands - chown Command

CSDN BlogδΈ¨Stars in the Sky

https://blog.csdn.net/myself88129

Leave a Comment