Linux Commands

How To Use Chown Command in Linux

Understanding how to manage file permissions is critical to system security as a Linux user. It also lets you organize your system effectively to ensure its smooth functioning. Linux contains various commands, such as chmod, chgrp, chown, and unmask, to modify file permissions.

Many beginners find the chown command a bit complicated and need to learn more about it. Hence, in this quick guide, we will explain various use cases on how to use the chown command in Linux.

How To Use Chown Command in Linux

The chown command lets you modify the ownership of those files. Here is the basic example of the chown command: 

chown [options] [new_owner] [new_group] file_name

In the above command, please replace the [new_owner] [new_group] with the user’s name to whom you want to transfer ownership of the respective file. It can be a solo user, a group, or both. Also, replace the file_name with the targeted file. 

For example, let’s use the chown to change the owner of the “file.txt”:

chown echo Info

chown-command-in-linux

This command, on execution, will make “echo” the new owner of the “file.txt” file. You can also use the above command for changing the directory owner. However, the changes will only apply to the parent directory. That’s why you must use the -R (recursive) option to apply these changes to a directory and all subdirectories. For example:

chown -R echo Info

recursive-option-in-chown-command

If you already have a file with your desired file owners and want to use the chown command to transfer ownership to them only. In this case, you can take owner references from other files using the — reference option. For instance:

chown --reference=file_1.txt file_2.txt

Finally, if you want to change the both owner and group of a file, you can run the below command:

chown echo:Work file.txt

change-both-owner-and-group-using-chown-command

A Quick Wrap-up

File permissions are critical to consider when maintaining the system’s security. Understanding them can help you prevent unintentional data loss. You can use the chown command to modify those permissions, but many users are unaware of it. Therefore, this guide explained the use of chown commands with the help of appropriate examples and use cases.

About the author

Prateek Jangid