In this article we are going to shows how to use chown command in Linux. If you want to learn about chown command in Linux with Examples then this post is ideal for you.
In the Linux, the chown command is stands for change owner and it is used to change the ownership of given files, directories and symbolic link for a user or group.
How to use chown Command in Linux
In the Linux, the basic syntax of the chown command is show as below:
chown [OPTIONS] [OWNER][:[GROUP]] FILE(s)
- OPTIONS = Options for Chown command.
- OWNER = Username or UID (UserID) of specific user.
- GROUP = Name of the group or the group ID.
- FILE(s) = file or multiple files.
You can use the ls -l
command to check out the permissions of files. When you use -l
option (long listing format) with ls
command, you will see the below file information:
- File type.
- File permissions.
- Number of hard links to the file.
- File owner.
- The file group.
- File size.
- Date and Time.
- File name.
ls -l file_name.txt
-rw-r--r-- 12 studiesonline users 22.0K Apr 15 22:11 file_name.txt
|[-][-][-]- [------] [---]
| |
| +-----------> Group
+-------------------> Owner
Change Ownership of a File or Directory
Syntax :
chown USER FILE
Run the command below to change the ownership of a file to a specific user:
chown studiesonline file_name.txt
To change the ownership of multiple files or directories, run the command below:
chown studiesonline file_name.txt file_name.txt1 dir1
You can also use user ID (UID) instead of username as show below:
chown 1100 file_name.txt
Change the Owner and Group of a File
The chown
command also allows you to change both the owner and the group of a file at once.
Syntax:
chown USER:GROUP FILE
The command below will change the ownership of a file file_name.txt
to a new owner studiesonline
and group linuxusers.
chown studiesonline:linuxusers file_name.txt
If you don’t provide the group name after the colon (:
) then by default the group of the file is assign to the specified user’s login group:
chown studiesonline: file_name.txt
How to Change the Group of a File
To change only the group of a file, use below syntax:
chown :GROUP FILE
The command below will change the ownership of a file named file_name.txt
to new group named study
:
chown :study file_name.txt
Recursively Change the File Ownership
You can use
with -R
(--recursive
) optionchown
command to change ownership of all file and sub-directories in a given directory.
Syntax:
chown -R USER:GROUP DIRECTORY
The command below change the ownership of all files and directories inside /var/www
to a new owner and group named www-data
:
chown -R www-data: /var/www
Use the -h
option, if the directory contains symbolic links:
chown -hR www-data: /var/www
Use --reference=ref_file
option to copy ownership of one file to another:
chown --reference=file_name.txt file_name.txt1
That’s all.
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.