How to Change the Owner of a Directory in Linux

Tux Linux

If you’re a newcomer to the world of Linux, you may be wondering how to change the owner of a directory. This is a common task, and with a few simple commands, you can easily change the directory owner to suit your needs. In this guide, we’ll walk you through the process step by step, using a convenient command to change the owner. We’ll also show you how to modify the command if you want to specify a different user as the owner.

Determining the Current Owner:

Before you can change the owner of a directory, you might want to know who the current owner is. To check this, open your terminal and follow these steps:

Step 1: Open Your Terminal

Click on the terminal icon in your Linux desktop environment or use the shortcut Ctrl + Alt + T.

Step 2: Check the Current Owner

Use the ls command followed by the -l option to list the contents of the directory with detailed information. For example, if you want to check the owner of a directory named “mydirectory,” you would type:

ls -l /path/to/mydirectory

The command will display a detailed list of the directory contents, including the owner’s name in the third column.

Changing the Owner:

Now that you know the current owner, let’s move on to changing it using the sudo chown command with the $USER variable.

Step 3: Use the chown Command

To change the owner of the directory to your current user, you can use the following command:

sudo chown -R $USER /path/to/directory
  • sudo: You’ll need superuser privileges to change ownership, so you’ll need to use sudo before the chown command.
  • -R: This option stands for “recursive,” which means it will change the owner of the specified directory and all its subdirectories and files.
  • $USER: This is a system variable that represents your current username. It’s a handy way to change the ownership to your own user account.
  • /path/to/directory: Provide the path to the directory you wish to change the owner of.

Step 4: Execute the Command

For example, if you want to change the owner of “mydirectory” to your user account, you would run the following command:

sudo chown -R $USER /path/to/mydirectory

After entering your password (since sudo requires superuser privileges), the owner of the directory and all its contents will be updated to your user account.

Modifying the Command for a Different User:

If you want to specify a different user as the owner of the directory, you can modify the chown command as follows:

sudo chown -R new_owner:new_owner_group /path/to/directory
  • sudo: You’ll still need superuser privileges, so sudo is necessary.
  • -R: This option remains the same for recursive changes.
  • new_owner: Replace this with the username of the user you want to assign as the new owner of the directory.
  • new_owner_group: This is the group of the new owner, which is typically the same as the new owner’s username.
  • /path/to/directory: Provide the path to the directory you wish to change the owner of.

Let’s say you want to change the owner of a directory named “sharedfiles” to a user named “otheruser” and you want to keep the group ownership the same as “otheruser” too. You can use the following command:sudo chown -R otheruser:otheruser /path/to/sharedfiles

After running this command and entering your password, the owner of the “sharedfiles” directory and all its contents will be updated to “otheruser,” and the group ownership will also be set to “otheruser.”

Verifying the Change:

To double-check that the owner has been changed, you can use the ls -l command again as we did in the first step. It should now display the new owner’s name in the third column.

Congratulations! You’ve successfully changed the owner of a directory in Linux using a straightforward command. This is a handy skill to have, especially when working on multi-user systems or managing various files and directories. Just remember to use the chown command with caution, as improper ownership changes can affect your system’s functionality.