CMD Master
Back to Blog
UpdatedArnošt Havelka

How to Change File Ownership with chown in Bash

Use chown to change a file or directory owner and group, verify the result, and avoid unsafe recursive ownership changes.

Start Interactive Lesson
How to Change File Ownership with chown in Bash

Use chown when a file or directory belongs to the wrong user or group. Inspect the target first, change one explicit path, and verify it immediately. Add -R only when every item below a directory should receive the same ownership. The Bash chown lesson lets you practise the basic form in an isolated filesystem.

Check the current owner and group

Before changing anything, inspect the exact path:

ls -ld project
ls -l project/readme.txt

In a long listing, the owner and group appear after the permission and link-count fields. Confirm that project is the intended directory, then confirm that the destination account and group already exist. chown does not create users or groups.

Change both owner and group

The common form places owner:group before the path:

sudo chown admin:developers project

This makes admin the owner and developers the group for the directory entry project. It does not recursively change the contents. Use sudo only when your current account lacks permission; elevated privileges do not protect against a misspelled name or path.

Verify the result:

ls -ld project

To change only the owner, omit the group:

sudo chown admin project/readme.txt

To change only the group with chown, leave the owner empty:

sudo chown :developers project/readme.txt

The dedicated chgrp guide is often clearer when group ownership is the only thing changing.

Treat recursive ownership as a separate decision

The -R option applies the change to the starting directory and everything below it:

sudo chown -R admin:developers project

Before pressing Enter, use pwd and ls -ld project to confirm the starting point. Avoid unresolved variables, wildcards, and paths copied from an untrusted message. Never experiment with recursive ownership from /, an entire home directory, or a production mount.

Afterward, verify both the directory and representative files:

ls -ld project
ls -l project/readme.txt

A tree can intentionally contain files owned by different service accounts. Replacing all of them with one owner may break deployments, databases, containers, or shared directories even when the command itself succeeds.

Know what chown does not fix

Ownership and permissions are separate. chown changes which user and group the permission bits refer to; chmod changes the permission bits. If the owner is correct but access is still denied, inspect the mode instead of repeating the ownership change.

Symlink traversal, network mounts, containers, and managed filesystems can also affect the result. Read the local man chown page before applying recursive changes to a tree containing links or mount points. If the filesystem rejects the operation, investigate its ownership model rather than adding broader privileges repeatedly.

For numeric access modes, continue with how chmod numbers work. GNU documents the complete ownership syntax and traversal options in the Coreutils chown reference.

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

chmod Numbers Explained: 600, 644, 700, and 755

Decode numeric chmod permissions, choose common file and directory modes, verify the result, and avoid unsafe recursive changes.