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.

Steps to change ownership of files and directories in Linux:

  1. Confirm that the target user and group exist before changing ownership.
    $ 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.

  2. Check the current owner and group of the file or directory that will be updated.
    $ 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.

  3. Change only the file owner and confirm that the group stays the same.
    $ 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.

  4. Change only the group owner of a file.
    $ 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.

  5. Change both the owner and group of a directory in one command.
    $ 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.

  6. Change ownership for the immediate contents of a directory without recursing into deeper levels.
    $ 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.

  7. Change ownership recursively for a directory tree when every nested file and subdirectory should be updated.
    $ 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.

  8. Match the ownership of another file when a known-good reference already exists.
    $ 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.

  9. Review the resulting ownership across the directory tree.
    $ 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.