How to Change the Owner of a Directory in Linux

Linux lets you control everything and that’s one of the reasons why almost every service in the world runs on servers that are managed using Linux. This pinpoint control is not only available on servers but also to users who use Linux distros on a daily basis. One of the advantages of having control is you could limit other users in Linux to how much information and files they can access, and if you didn’t already know you could do that, here’s how to change the ownership of a directory in Linux.

How to Check the Owner of a File or Directory in Linux

Before we delve into the command to change directory ownership in Linux, it is important to know how to check who is the current owner of a directory. To check if a file or directory belongs to you or others in the group, all you need to do is use the following command.

ls -l "File name" or
ls -l "Directory name"
directory current owner

This should give you the following output which tells you whom the file or directory belongs to. In the above example,

You can then proceed with transferring the ownership.

Change Directory Owner in Linux using Chown Command

Linux commands, from their names, are pretty straightforward to guess and explain and the same goes with Chown. It stands for change ownership and it does exactly what the name suggests, i.e., change the ownership of directories and files. Here’s the basic syntax of the chown command.

sudo chown -R $USER /directory
  • Chown stands for Change Directory
  • -R stands for Recursive and helps change the ownership of the sub-directories
  • $USER is your username
  • /directory should be replaced by the directory you want to change the ownership of.

An example of a valid chown command would be the following:

sudo chown -R abubakarmohammed /example_directory
Changed Directory Owner

Considering the /example_directory is owned by “/” or the superuser, using the above command would transfer the ownership to the user “abubakarmohammed.” Failing to use -R will result in internal, nested directories still belonging to the previous owner.

If you want to transfer the access back to the superuser, use the following command.

sudo chown -R root: example_directory
changing directory owner to root

Using “-R” is crucial here too because it recursively applies the chown command to all the internal files. Forgetting to use the same would mean the original directory would be transferred but its contents would still remain accessible to other users to modify.

Change File Owner in Linux using Chown command

As expected, Chown is not limited to directories but also applies to files. The basic syntax for changing file ownership in Linux is as simple as it can get.

sudo chown $USER file1 file2

An example of this is changing the ownership of files called 4.txt and 5.txt from the user “abubakarmohammed” to superuser.

sudo chown abubakarmohammed 4.txt 5.txt
Change ownership of text files

Not all usernames are the same and some can be exhaustive to type out multiple times. Hence, an alternative to typing usernames is using user IDs. All you need is to find out the user ID of the user you want to transfer the ownership to and replace it with the username. To find the user ID of a user, use the following command.

id -u $USER
Get User ID command

Now that you know the user ID, use it in the chown command in the following way:

sudo chown user_id file1

As per the above syntax, the command to transfer ownership of a file in Linux using user ID would be:

sudo chown 1000 4.txt
Using Chown command with user ID
comment Comments 1
  • emma jones says:

    To change the owner of a directory in Linux, you can use the “chown” command followed by the new owner’s username and the directory path.

Leave a Reply