How to Change Ownership of Files and Folders Recursively
Learn how to use the chown command to recursively change the user and group ownership of files and directories in Linux command line.
You can use the chown command in Linux to change the ownership of the file(s) and directories. It's quite simple to use.
chown owner_name file_or_folder
The problem arrives when you change the ownership of a directory, its content remains unchanged. The solution is not too complicated as well.
To change the ownership of all the contents of a directory, you can use the recursive option -R with chown command:
chown -R owner_name folder_name
If you want to change both owner and group recursively, you can use it in the following manner:
chown -R owner_name:group_name folder_name
Let's see it in detail and also see how you can change user and group recursively. Things are a lot easier to understand if you are familiar with the concept of file ownership and permission.
To recursively change the ownership of a directory, use it like this:
chown -R new_owner_name directory_name
If you have to change the ownership of multiple directories with their contents, you can do it in the same line:
chown -R new_owner_name directory1 directory2 directory3
Let me show that with a sample example. I have a directory named new_dir with some content in it. This directory and its content are owned by the root user.
root@learnubuntu:~# ls -l /home/abhi/ total 4 drwxr-xr-x 3 root root 4096 May 30 07:30 new_dir root@learnubuntu:~# ls -l /home/abhi/new_dir/ total 20 -rw-r--r-- 1 root root 12813 May 30 07:30 new.txt drwxr-xr-x 2 root root 4096 May 30 07:30 one_more_dir
When I change the ownership of the new_dir, its ownership is changed:
root@learnubuntu:~# chown abhi /home/abhi/new_dir root@learnubuntu:~# ls -l /home/abhi/ total 4 drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
But the files and folders inside it are still owned by root.
root@learnubuntu:~# ls -l /home/abhi/new_dir/ total 20 -rw-r--r-- 1 root root 12813 May 30 07:30 new.txt drwxr-xr-x 2 root root 4096 May 30 07:30 one_more_dir
Now, if I use the recursive option -R with chown command, it changes the ownership for everything inside the specified directory, not just the directory.
root@learnubuntu:~# chown -R abhi /home/abhi/new_dir root@learnubuntu:~# ls -l /home/abhi/new_dir/ total 20 -rw-r--r-- 1 abhi root 12813 May 30 07:30 new.txt drwxr-xr-x 2 abhi root 4096 May 30 07:30 one_more_dir
Change both owner and group recursively
The chown command allows you to change the owner as well as the group of files.
To recursively change the owner and group of a directory and all its content, use the chown command like this:
chown -R user_name:group_name directory_name
You can use the same for changing the ownership of multiple folders:
chown -R user_name:group_name dir1 dir2
Not too difficult, eh?
Recently, I moved a self-hosted Ghost instance to a new server launched with DigitalOcean's 1-click deployment. I had to upload the entire images folder from the backup (downloaded on the local system) to the new server. The system required changing the ownership of this image folder from root to ghost.
This method saved me the trouble. I hope this quick little tutorial helps you as well.
I highly recommend brushing up the basics of file permissions and ownership.
It's one of the essential Linux concepts you must know.