recursively change directory owner in linux

How to Recursively Change Directory Owner in Linux

Sometimes you may need to recursively change directory owner in Linux. This is required especially if you are installing a web server or CMS such as WordPress, Magento, Joomla, etc on your server. If all the subdirectories and files in the installed directory don’t have the appropriate owner, then your website may not work properly. You can easily change directory ownership using chown command. In this article, we will look at how to recursively change directory owner in Linux.


How to Recursively Change Directory Owner in Linux

Here are the steps to recursively change directory owner in Linux.

You can change directory ownership recursively using -R option, followed by the locations of folders whose ownership you want to change, in a space-separated manner.

$ chown -R <owner> <folder_1> <folder_2> ... <folder_n>

Here is an example to change ownership of files & subdirectories in folder /var/www/html/wordpress to www-data

$ chown -R www-data /var/www/html/wordpress

Here is the command to change ownership of three folders.

$ chown -R www-data /var/www/html/wordpress /home/data /etc/data

If you want to recursively change owner as well as group for a folder, then mention the username & group separated by colon.

$ chown -R <user>:<group> <folder_1> <folder_2> ... <folder_n>

Here is an example to change user and group ownership of /var/www/html/wordpress to www-data and root respectively.

$ chown -R www-data:root /var/www/html/wordpress

Please note, if you have multiple target files & folders whose ownership you want to change, then it is advisable to use command like find or ls to get a proper list of files & subfolders that you need to update. Otherwise, you may end up changing ownership of unnecessary files & folders, causing some of your applications to stop working.

In this article, we have learnt how to recursively change ownership of directory & files. This is a common requirement when installing WordPress, Magento and other CMS or blogging platforms.

Also read:

How to Append Text at End of Each Line
How to Find Out Who is Using File in Linux
How to Find & Remove Unused Files in Linux
How to Sort Files by Size in Linux
How to Combine Multiple PDF Files into One in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *