Linux chown Example

Linux chown example

chown is a linux command used to change the owner / Owner group of a file or a directory in a Linux environment. Chown is the shorter version for “Change Owner”, and it has an easy syntax so you can enter it quickly. In this article, you will see the most generic form of the usage of this command.

In order to change the owner of a file or a directory, you need to have the root privileges of the system, or you should be the owner of that particular file or directory. If not you can use the sudo prefix in front of your command to run the command as root.

Basic Syntax :

chown [OPTION]... [OWNER][:[GROUP]] FILE...

Before we move in to our examples, let's see how we can check the current owner of the file.

Enter the following command :

ls -l

It will output something similar to this.
-rw------- 1 nimeshka nimeshka 155245 Aug 12 09:55 1.png

-rw------- 1 nimeshka nimeshka  30948 Aug 12 10:09 2.png

-rw------- 1 nimeshka nimeshka 466892 Aug 12 10:18 3.png

the format of the above output is as below;

[permissions] [owner] [group] [size] [modified date & time] [file/directory name]

Example 01 : To Change Owner

chown jeff 1.png

This will assign the ownership of 1.png file to jeff user.

Example 02 : Change the Group

chown :developer 1.png

This will keep the current owner, but change the group of the file.

Example 03 : Changing both owner and group

chown jeff:developer 1.png

You can see that the owner and the group is separated by a colon (:)

Example 04 : Changing the owner of a directory recursively.

chown -R jeff /my/directory

Notice the -R option we passed to the command. -R tells the command to go through all the files and directories inside /my/directory and apply the changes.

If you want to learn more about this command, view the manual page using the command:

man chown

Please share this article with your friends if you find it useful :)


Post a Comment