Linux chown command

Linux chown command is used to change a file's ownership, directory, or symbolic link for a user or group. The chown stands for change owner. In Linux, each file is associated with a corresponding owner or group.

The Linux system may have multiple users. Every user has a unique name and user ID. If only a user is available in the system, the user will be the owner of each file.

The Linux system may have multiple users. Every user has a unique name and user ID. If only a user is available in the system, the user will be the owner of each file.

Users can be listed in different groups. The group allows us to set permission on the group level instead of setting permission on an individual level.

Syntax:

Below is the general syntax of the chown command:

Options:

Following are the command-line options of the chown command:

-c, --changes: It is used to display the detailed output like verbose, but it is reported when only a change is made.

-f, --silent, --quiet: It is used to suppress the error messages.

-v, --verbose: It is used to display a diagnostic for every processed file.

--dereference: It is used to affect the referent of each symbolic link.

-h, --no-dereference: It is used to affect the symbolic links instead of any referenced file.

--from=CURRENT_OWNER:CURRENT_GROUP: It is used to change the specific owner and group.

--no-preserve-root: It is used for not treating the backslash ('/') especially.

--preserve-root: If the chown is failed to operate recursively on backslash ('/').

--reference=RFILE: It is used to specify the RFILE's owner and group rather than their values.

-R, --recursive: It is used to perform operations on files and directories recursively.

--help: It is used to display the help manual having a brief description of the usage and options.

--version: It is used to display the version information.

Display the UID, GID, and Groups

To display all the groups, execute the groups command as follows:

The above command will list all the existing groups from your Linux system. Consider the below output:

Linux chown command

To list the UID and GID, execute the id command as follows:

The above command will list all the UID and GID of their corresponding users and groups. Consider the below output:

Linux chown command

Display the User and Group Ownership of a File

To display the user and group owner of a file, execute the "ls -l" command with a specific file name. Consider the below command:

The above command will display the user and group ownership of the 'Demo.txt.' Consider the below output:

Linux chown command

Change the owner of a File (Using user name)

To change the owner of a file, pass the user name (new owner) with the chown command as follows:

Consider the below command:

The above command will set the jtp as the owner of the file 'Demo.txt'. Consider the below output:

Linux chown command

From the above output, the owner of the given file has changed.

Change the Owner of a File (Using UID)

We can change the owner of a file or directory using the UID of a user. To change the ownership by using the UID, pass the UID with chown command as follows:

The above command will change the ownership of the 'Demo.txt'. Consider the below output:

Linux chown command

Change the Group of a File

To change the group of a file, specify group name or GID with the chown command. It represents similar behavior as chgrp command. Execute the chown command specifying the group name separated by a colon as follows:

or

Consider the below command:

The above command will change the group of 'Demo.txt.' Consider the below output:

Linux chown command

From the above output, we have changed the group using the group name in the first command. In the second command, we have changed the group name using the GID.

Change the User and Group Name

We can change the user and group names together. To change the user and group name at once, execute the chown command as follows:

Consider the below command:

The above command will change the specified user name and group name together. Consider the below output:

Linux chown command




Contact US

Email:[email protected]

Linux chown command
10/30