How to Change Ownership of Directory in Linux?

In Linux, the ownership consists of four kinds of rights such as “right to use the file/directory”, “right to exclude others from using the file”, “right to dispose of the file” and in the last “right to destroy the file”. When we run the  “ls -l” command in Linux, it shows all the basic information about all the files and directories in which the owner/username and the group name of that file or directory are mentioned. The user can change the ownership of the current user and transfer it to another new or existing user with the help of the “chown” command.

This article will demonstrate the possible ways to change the ownership of the directory in Linux.

How Does the chown Command Work?

The “chown” command is the “change ownership” command. The working of the “chown” command depends on its syntax, which is explained below:

Syntax

The general syntax of the “chown” command is written below:

chown [option]... [owner][:[group]] FILE...

The “chown” is the main keyword in the syntax. However, the square brackets show the “option”, “owner”, and the “group” of the specific file.

The “chown” command provides a list of essential supported options you can get by utilizing the “help” command:

$ chown --help

Scroll down the page to see more options.

Let’s move to the working of the “chown” command.

How to Change Ownership of Directory in Linux?

The main objective of the “chown” command is to change ownership and assign the rights to specific users. This section explores various scenarios for changing the ownership of a directory in Linux. So, let’s start with the first example:

Note: Here, the “ls -l” command is used to verify the change in ownership or list the current owners of files/directories.

Example 1: Changing Ownership of a Directory

In Linux, the “ls -l” command displays the detailed view of the current “Home” directory content in table format as shown below:

$ ls -l

In the above output of the “ls -l” command, the second column represents the “user name”, while the third column denotes the “group name”. For example, the highlighted current directory “Documents” is owned by “itslinuxfoss”.

Now, use the “chown” command to change the selected directory ownership. For this purpose, type the chown command with the new username, i.e., “johnson”, and the specific directory name and press enter key:

$ sudo chown johnson ./Documents/

The ownership of the “Document” directory has been changed. Hence, the “Documents” directory is now owned by “johnson”.

Note: The mentioned user must exist in the current system. Moreover, the “ls” command is used with each example to verify the permissions.

Example 2: Changing Ownership of Subdirectories

The “chown” command is also beneficial for changing the subdirectories’ ownership. Suppose the “Directory” contains three subdirectories as shown in the screenshot via the “ls -l” command:

$ ls -l

All the subdirectories have the same user name, “herry”, highlighted in the above screenshot. To change it, run the “chown” with the “-R” option on the terminal:

$ sudo chown -R peter ./Directory1/

The “-R” option (recursive) is used for performing the different operations on the files and subdirectories located in a directory. In our case, it changed the ownership of the “Directory” from “herry” to “peter”.

Example 3: Changing Ownership of Directories With UID and GID

The user can change the user name and the group name of the particular directory with the “UID(unique identifier)” and “GID (group identifier)” instead of the user and group name. Suppose the “Directory2” has been owned by “itslinuxfoss” as shown below:

$ ls -l

Type the “chown” command and enter the “UID” and “GID” with the directory name and press enter:

$ chown 1003:1003 Directory2

The ownership of the “Directory2” is now changed by the new user “peter”.

Example 4: Changing Ownership of All Files/Directories in PWD

Changing the ownership of the directory and files separately is generally a time-consuming task. To get rid of this problem, the “chown” command helps the user to change the ownership of files and directories at once. Suppose the “Notes” directory has two files and one subdirectory as shown below:

$ ls -l

Now type the “chown” command with a new user “herry” and the files&directory name. It will change the ownership of the mentioned file and directory at a time:

$ sudo chown herry FirstFile.txt FirstFile2.txt impnotes

The output confirms that now the “herry” is the owner of “FirstFile2”, “FirstFile.txt”

and the directory “impnotes”.

Bonus Tip: How to Change the Ownership of Files?

The ownership of the files can also be done in the same way as directories. Here, in this section, we will discuss practical examples of changing the ownership of single and multiple files.

Example 1: Changing the Ownership of a Single File

Run the “ls -l” command to pen down the information of “file1.txt” in the “Docs” directory:

$ ls -l file1.txt

Here, the output shows that the owner/user name of “file1.txt” is “itslinuxfoss”. At this point, execute the “chown” command with the new username and file name to change its ownership:

$ sudo chown anna file1.txt

The owner of the “file1.txt” is now changed and has the rights to read, write and execute this file.

Example 2: Changing Ownership of Multiple Files

Same as the single file, the ownership of multiple files can also be changed. Continuing with the “Docs” directory, there are three files whose ownership is owned by the same user, “itslinuxfoss”:

$ ls -l

Enter the “file2”, “file3”, and “file4” files with the “chown” command in the terminal for changing the ownership of these files:

$ sudo chown anna file2.txt file3.txt file4.txt

The output verifies that now the username or the ownership of the above files has been changed to the highlighted user “anna”.

That’s all about this article!

Conclusion

In Linux, the ownership of directories can be changed by utilizing the “chown” own command. The “chown” command is the short form of “change ownership”. The “chown” command is also beneficial for changing the ownership of the available files and subdirectories in that particular directory. Moreover, it supports a large list of options used to perform the different operations on files and directories. In this article, all the possible examples are described to change the ownership of directories and files.