Changing ownership of files and directories in Linux decides which account controls a path and which group can share it. This matters after restoring backups, moving application data, unpacking archives as root, or handing a directory to a service account that must be able to read and write it.
Every file and directory stores both a user owner and a group owner. Commands such as chown and chgrp change those metadata fields without changing the file contents, while tools such as stat, ls -ld, and find show the current ownership before and after the change.
Most ownership changes require sudo or a root shell, and some systems limit non-root group changes to groups the caller already belongs to. Recursive ownership changes should be aimed at the exact directory path, because a mistaken -R run can break application access, and some systems also clear setuid or setgid bits when ownership changes. Replace sample names such as appuser and ops with the real account and group from the target system.
$ getent passwd appuser appuser:x:1001:1001::/home/appuser:/bin/sh $ getent group ops ops:x:1002:
The getent command checks the active account database, including local users and directory-backed identities, so the ownership change uses valid names.
$ stat -c "%n %U:%G" /srv/app/current /srv/app/current/owned.txt /srv/app/current root:root /srv/app/current/owned.txt root:root
The stat format string prints the path followed by its current user:group ownership so the change can be verified later.
$ sudo chown appuser /srv/app/current/owned.txt $ stat -c "%n %U:%G" /srv/app/current/owned.txt /srv/app/current/owned.txt appuser:root
Supplying only a user name to chown changes the owner field and leaves the existing group unchanged.
$ sudo chgrp ops /srv/app/current/owned.txt $ stat -c "%n %U:%G" /srv/app/current/owned.txt /srv/app/current/owned.txt appuser:ops
The chgrp command updates just the group field. The equivalent chown :ops /srv/app/current/owned.txt syntax works as well.
$ sudo chown appuser:ops /srv/app/current $ stat -c "%n %U:%G" /srv/app/current /srv/app/current appuser:ops
Use the user:group form to set both fields together. If you run chown appuser: path with nothing after the colon, chown switches the group to that user’s login group.
$ sudo chown appuser:ops /srv/app/current/* $ find /srv/app/current -maxdepth 2 -printf "%y %p %u:%g\n" | sort d /srv/app/current appuser:ops d /srv/app/current/docs appuser:ops f /srv/app/current/docs/readme.txt root:root f /srv/app/current/owned.txt appuser:ops
The shell expands \* before chown runs, so the command changes only the matched entries. Hidden names such as .env are not matched by default, and nested files remain unchanged unless the parent directory itself was selected.
$ sudo chown -R appuser:ops /srv/app/current $ find /srv/app/current -maxdepth 2 -printf "%y %p %u:%g\n" | sort d /srv/app/current appuser:ops d /srv/app/current/docs appuser:ops f /srv/app/current/docs/readme.txt appuser:ops f /srv/app/current/owned.txt appuser:ops
Double-check the full path before using -R. A recursive ownership change on the wrong directory can break services or expose data, and following directory symlinks intentionally requires extra options that should be used with care.
$ sudo chown --reference=/srv/app/current/reference.txt /srv/app/current/docs/readme.txt $ stat -c "%n %U:%G" /srv/app/current/reference.txt /srv/app/current/docs/readme.txt /srv/app/current/reference.txt root:root /srv/app/current/docs/readme.txt root:root
The –reference option copies ownership from an existing path, which helps keep related files consistent without retyping the intended user and group.
$ find /srv/app/current -maxdepth 2 -printf "%y %p %u:%g\n" | sort d /srv/app/current appuser:ops d /srv/app/current/docs appuser:ops f /srv/app/current/docs/readme.txt root:root f /srv/app/current/owned.txt appuser:ops f /srv/app/current/reference.txt root:root
A final tree listing makes it clear which paths inherited the recursive change and which ones were intentionally reset from a reference file.