Linux chown Command: 10 Examples with Details

The chown (change owner) command in Linux is used to change the ownership of files and directories. It can modify both the user and group ownership. Below are 10 practical examples demonstrating various uses of the chown command, complete with explanations and scenarios. Note: Most examples require root privileges (use sudo).

Example 1: Change File Owner

Change the owner of a single file to a specific user.

sudo chown john document.txt

Explanation: This command changes the owner of document.txt to the user john. The user must exist on the system.

Use Case: Transferring ownership of a file to a team member who needs to manage it.

Example 2: Change Owner and Group

Change both the user and group ownership of a file.

sudo chown john:developers document.txt

Explanation: This command sets the owner of document.txt to john and the group to developers. The format is user:group.

Use Case: Assigning a file to a specific user and a group for collaborative access.

Example 3: Change Directory Owner Recursively

Change the owner of a directory and all its contents.

sudo chown -R john /home/user/projects

Explanation: The -R flag applies the ownership change recursively to /home/user/projects and all files and subdirectories within it.

Use Case: Transferring ownership of an entire project directory to a new user.

Example 4: Change Group Only

Change only the group ownership of a file or directory.

sudo chown :developers document.txt

Explanation: By omitting the user and using :group, this command changes only the group ownership to developers, leaving the user unchanged.

Use Case: Granting group access to a file without altering the user owner.

Example 5: Change Ownership of Multiple Files

Change the owner of multiple files at once using a wildcard.

sudo chown john *.txt

Explanation: This command changes the owner of all files with the .txt extension in the current directory to john.

Use Case: Bulk updating ownership for a set of similar files, like text documents.

Example 6: Copy Ownership from Another File

Use the ownership of one file as a reference for another.

sudo chown --reference=template.txt document.txt

Explanation: The --reference option copies the user and group ownership of template.txt to document.txt.

Use Case: Ensuring consistent ownership across files in a project.

Example 7: Change Ownership with Numeric UID and GID

Use user ID (UID) and group ID (GID) to set ownership.

sudo chown 1001:1002 document.txt

Explanation: This command sets the owner to the user with UID 1001 and the group with GID 1002. Use id -u username or id -g groupname to find IDs.

Use Case: Scripting scenarios where usernames or group names are not available.

Example 8: Change Ownership Without Following Symlinks

Change the ownership of a symbolic link itself, not the target.

sudo chown -h john symlink_to_file

Explanation: The -h flag ensures the ownership of the symbolic link symlink_to_file is changed, not the file it points to.

Use Case: Managing permissions for symbolic links in complex directory structures.

Example 9: Change Ownership of Files in a Directory Non-Recursively

Change ownership of files in a directory without affecting subdirectories.

sudo chown john /home/user/projects/*

Explanation: The * wildcard applies the ownership change to all files and directories directly inside /home/user/projects, but not to subdirectories' contents.

Use Case: Updating ownership for top-level files in a directory without modifying nested content.

Example 10: Verbose Output for Ownership Changes

Display detailed output while changing ownership.

sudo chown -v john:developers document.txt

Explanation: The -v (verbose) flag shows details of the ownership change, confirming that document.txt is now owned by john and the developers group.

Use Case: Debugging or logging ownership changes in scripts or manual operations.

Additional Notes