This quick tip shows you how to change the ownership of files and folders in Ubuntu. This will work in all versions of Ubuntu, including Ubuntu 11.10 (Oneiric Ocelot).
When you use the sudo command to install packages or create folders, the root account automatically assumes ownership. And if a regular user wants to edit or modify the file of which the root is the owner, he or she will be denied. In this case, you’ll have to give ownership of the file to the user who will be modifying it, and this is why this tutorial is here.
Without wasting anymore of your time, let’s continue below to learn how.
- Change ownership of files and folders in Ubuntu
To get started, press Ctrl – Alt – T on your keyboard to open Terminal.
The first thing you might want to do is list the content of the current directory. If you’re a new user, run the ls command.
The ls command lists the content of the current directory.
To change the ownership of a single file, run the command below.
sudo chown <username>:<groupname> filename
Replace <username> with the username of the account you wish to take ownership of the file. And <groupname> is the group that will assume ownership of the file.
By default, when a new account is created in Linux, a group is also created named after the username of the account. For example, if a user account is created with username <pumpkin>, a group will automatically be created named <pumpkin> and the user is automatically made a member of that group.
So, if you run the commands below, you’re giving ownership to the user <pumpkin> and the <pumpkin> group.
sudo chown pumpkin:pumpkin file.txt
But wait, you don’t always need to specify a group everytime you wish to change the ownership of a file. You could just change the username for a particular file and the group can stay intact.
sudo chown <username> <filename.txt>
Now that you know how to change the ownership of a single file, the below commands show you how to change the ownership of a folder and all sub-folders within.
sudo chown -R <username>:<groupname> FolderName
That’s it! And I hope you liked it.