How to Use chown Recursively on Linux
In this tutorial, you'll learn about How to Use chown Recursively on Linux. We cover key concepts, practical examples, and best practices.
The Problem
You copy files as root or extract an archive and now a directory tree has the wrong owner. Applications cannot write to their own directories because ownership is set to the wrong user or group.
Quick Fix
Step 1: Change owner recursively
Change the owner of a directory and all its contents to a specific user:
sudo chown -R www-data /var/www/myapp
The -R flag makes chown operate recursively on every file and subdirectory.
Step 2: Change owner and group at once
Set both user and group in one command:
sudo chown -R www-data:www-data /var/www/myapp
Step 3: Change only the group
Use a colon prefix to change only the group:
sudo chown -R :www-data /var/www/myapp
Step 4: Verify the changes
Check the ownership of files in the directory:
ls -la /var/www/myapp
drwxr-xr-x www-data www-data ...
-rw-r--r-- www-data www-data ...
Step 5: Use chown with specific file patterns
Change ownership only for certain file types:
sudo find /var/www/myapp -type f -name "*.log" -exec chown www-data:www-data {} \;
Step 6: Avoid common pitfalls
Do not use a trailing slash with -R:
# Works correctly
sudo chown -R user:group /path/to/dir
# Same result, but inconsistent
sudo chown -R user:group /path/to/dir/
Also, note that chown -R changes ownership of symbolic link targets, not the links themselves. Use --no-dereference to change the link:
sudo chown -hR user:group /path/to/dir
Alternative Solutions
Use chgrp -R to change only the group
Change group ownership without affecting user owner:
sudo chgrp -R www-data /var/www/myapp
Use ACLs for more granular permissions
Set permissions for multiple users without changing ownership:
sudo setfacl -R -m u:developer:rwx /var/www/myapp
Common Mistakes to Avoid
Using chown -R on system directories without caution. Changing ownership of /usr or /etc can break the system. Only chown application-specific directories.
Forgetting to use sudo. chown requires root privileges. Without sudo, it fails with Operation not permitted.
Not preserving symbolic links. By default, chown -R follows symlinks and changes the target's ownership. Use -h to change the link itself.
Pro Tips
Use chown --from to limit scope. Only change ownership if the current owner matches: chown -R --from=root:root www-data:www-data /var/www.
Use rsync with --chown for transfers. When copying files, set ownership during the transfer: rsync -a --chown=www-data:www-data /src/ /dst/.
Use getfacl and setfacl for complex permissions. Access control lists allow multiple users and groups with different permissions on the same files.
Prevention
- Use
chown -Ronly when needed -- prefer setting correct ownership at file creation time. - Run
ls -labefore and after to verify ownership changes. - Use
chown --from=current-user:current-groupto change ownership only if it matches a specific user or group.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro