How-to

Change Ownership of a Whole Folder Tree

After a migration, a restore or a new deploy user, files often belong to the wrong account. chown -R fixes that in one pass when you pick the right flags.

What is the command?

chown -R deployer:deployer /srv/shop

This makes deployer the owner and deployer the group of /srv/shop and everything inside it. Only root, or a user with sudo, may give files to another account. A trailing colon with no group, as in chown -R deployer: /srv/shop, sets the group to the user's login group. To change only the group, use chown -R :www-data /srv/shop or chgrp -R www-data /srv/shop.

When do I need a recursive chown?

  • After restoring a backup as root, when files come back owned by root.
  • After copying a site from another machine where user IDs differ.
  • When handing an app over from one deploy user to another.
  • When a process ran as the wrong user and created files the real service cannot change.

The second case is subtle: Linux stores owners as numbers, not names. If deployer is user 1001 on the old machine and 1002 on the new one, files copied with numeric IDs show up owned by whatever account has 1001 locally, or as a bare number. A recursive chown fixes the mismatch.

How does chown handle symbolic links?

By default chown -R changes the link itself, not what it points to, and does not descend into linked folders. That is the safe behavior. Three flags change it:

FlagBehavior with -R
-P (default)Never follow symlinks
-HFollow a symlink only if it was named on the command line
-LFollow every symlink to a folder while walking

Avoid -L unless you are sure where every link leads. A single link to /etc or /home inside a project folder would hand those trees to the project user.

Can I change only files owned by a certain user?

Yes. GNU chown has --from, which acts only on files whose current owner, group or both match:

chown -R --from=root:root deployer:deployer /srv/shop
chown -R --from=1001 deployer /srv/archive

The first line hands over only the files root left behind and keeps anything already owned by the service account. The second fixes leftovers from an old numeric ID.

find gives even finer control:

find /srv/shop -nouser -exec chown deployer: {} +
find /srv/shop -group root -type d -exec chgrp www-data {} +

-nouser catches files whose owner ID matches no account at all, the typical leftover from copies between machines.

What ownership should a web app have?

Code owned by a deploy user, writable folders owned by the service. This way a compromised PHP or Node process cannot rewrite the application itself:

chown -R deployer:www-data /srv/shop
chown -R www-data:www-data /srv/shop/storage /srv/shop/public/uploads
chmod -R u=rwX,g=rX,o= /srv/shop

On Rocky Linux and AlmaLinux the web user is nginx or apache rather than www-data. File modes for this layout are covered in chmod recursively.

Does chown change SELinux labels or ACLs?

No. Ownership, mode bits, ACLs and SELinux labels are separate. After moving files into place on an Enterprise Linux image, run restorecon -Rv on the tree so services are allowed to read them. If the tree uses ACLs, check them with getfacl; chown leaves them unchanged.

How fast is it on big trees?

chown only rewrites metadata, so it is quick: on NVMe drives, a million files finish in well under a minute. Add -c to print only real changes, useful for logging. On a running service, change ownership during a quiet period or with the service stopped, so files created mid-run do not end up with mixed owners.

How can I prove no file was missed?

find /srv/shop -not -user deployer -not -path '/srv/shop/storage*' -not -path '/srv/shop/public/uploads*' | head
stat -c '%U:%G %n' /srv/shop /srv/shop/storage

The first command should print nothing. Command details and more examples are in The chown command.

Keep Reading

The chown command

Every useful form of chown: owner, group, both, numeric IDs, reference files and the rules about who may change ownership.

Open

chmod recursively

Change permissions across whole folder trees without breaking them: chmod -R, the capital X trick and find for files versus folders.

Open

Linux file permissions

Read ls -l output, understand owner, group and other bits, umask defaults, ACLs and why root and SELinux sit on top of it all.

Open

AlmaLinux

AlmaLinux 9 for hosting panels and software written for the Red Hat family, with no subscription to buy.

Open

FAQ

Quick Answers

Run chown -R user:group followed by the folder path, as root or with sudo.

Not by default. -H follows links named on the command line and -L follows all of them.

Use chown -R --from=root:root with the new owner and the path.

The numeric owner ID has no matching account on this machine. Fix it with chown or find -nouser.

No. Labels are separate; run restorecon -Rv on the tree after moving files.