Users Online

· Guests Online: 37

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

10+ Practical chown Command Usage Examples in Linux

This tutorial explains Linux "chown"? command, options and its usage with examples. "chown"? command is used to change file owner and group. This post describes "chown"? command used in Linux along with usage examples and/or output.

 

Usage: chown [OPTION] - ¦ [OWNER][:[GROUP]] FILE… chown [OPTION] -¦ –reference=RFILE FILE…

 

chown? is a command to change the ownership of a file/folder or even multiple files/folders for a specified user/group. chown stands for change file owner and Group. Here's the listing of example usage of command. :

 

1. To change the owner of a file(chown owner_name file_name):

 

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:12 1.txt

 

sanfoundry-> sudo chown root 1.txt

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

 

 

2.To change the group of a file(chown :group_name file_name): chown? command is also used to change group name of a file, like chgrp? command.

 

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

sanfoundry-> sudo chown :users 1.txt

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root users 0 Jun 21 12:12 1.txt

 

 

 

3. To Change both owner and the group(chown owner_name:group_name file_name):

 

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root users 0 Jun 21 12:12 1.txt

sanfoundry-> sudo chown himanshu:user 1.txt

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 himanshu user 0 Jun 21 12:12 1.txt

 

 

4. To change the owner from particular owner only(chown from ¦.): chown command is used to change owner from a particular owner name only.

 

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 himanshu user 0 Jun 21 12:12 1.txt

sanfoundry-> sudo chown --from=users root 1.txt chown: invalid user: `users'

sanfoundry-> sudo chown --from=himanshu root 1.txt

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root user 0 Jun 21 12:12 1.txt

 

Here as you can see --from=users ? has shown error since file 1.txt was owned by himanshu? not users. With the next command the owner is successfully changed from “himanshu� to “root�.

 

 

5. To change the group from particular group only(chown –from ….):

 

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root user 0 Jun 21 12:12 1.txt

sanfoundry-> sudo chown --from=:user :himanshu 1.txt

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

 

Here as you can see that group name “user� has been changed from “user� to “himanshu�.

 

6. To recursively change ownership of directories and their contents(chown -R owner_name folder_name):

 

sanfoundry-> ls -l sample/

 

total 0 -rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:29 x -rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:29 y

sanfoundry-> sudo chown -R root sample/

sanfoundry-> ls -l sample/

total 0 -rw-rw-r-- 1 root himanshu 0 Jun 21 12:29 x -rw-rw-r-- 1 root himanshu 0 Jun 21 12:29 y *

 

As, Here the owners are changed, groups can also be changed in recursive manner.

 

7. To change the ownership permissions forcefully/silent/quiet and don’t print any error(chown -f owner.. file_name):

 

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

sanfoundry-> chown himanshu 1.txt chown: changing ownership of `1.txt': Operation not permitted sanfoundry-> chown -f himanshu 1.txt

sanfoundry-> ls -l 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

 

Here as you can see that, with the addition of “-f� option the error message has been suppressed. But the operation has still not executed properly.

 

8. To copy the owner/group permissions from one file to another(chown –reference=original_file target_file): With the help of –reference option, You can transfer the settings of one file to another.

 

sanfoundry-> ls -l 1.txt 2.txt

 

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

-rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:40 2.txt

 

sanfoundry-> sudo chown --reference=1.txt 2.txt

sanfoundry-> ls -l 1.txt 2.txt

 

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:40 2.txt

 

Here as you can see, that the owner/group settings of file

1.txt are transfered to file

2.txt directly.

 

9. List all the changes made by the chown command in a verbose manner(chown -v ..):

 

sanfoundry-> ls -l 1.txt

 

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

 

sanfoundry-> sudo chown -v himanshu:users

 

1.txt changed ownership of `1.txt' from root:himanshu to himanshu:users

 

 

10. To change owner/group of a symbolic link(chown owner/group link_name):

 

sanfoundry-> ln -s 1.txt 1_link.txt

sanfoundry-> ls -l 1_link.txt 1.txt

 

lrwxrwxrwx 1 himanshu himanshu 5 Jun 21 12:45 1_link.txt -> 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:12 1.txt

 

sanfoundry-> sudo chown root:root 1_link.txt

sanfoundry-> ls -l 1_link.txt 1.txt

 

lrwxrwxrwx 1 himanshu himanshu 5 Jun 21 12:45 1_link.txt -> 1.txt

-rw-rw-r-- 1 root root 0 Jun 21 12:12 1.txt

 

As here you can see that applying chown command to change owner/group of a symbolic file, the owner/group of the file represented by the linked file is changed.

 

For e.g here changing settingd for 1_link.txt file does not caused any change in 1_link.txt, but the caused change in 1.txt file.

This is a default behavior of chown command.

 

11. To forcefully change owner/group of a symbolic file(chown -h …):

 

With the “-h� option default behavior of chown command is suppressed and owner/group of symbolic file is changed.

 

sanfoundry-> ls -l 1_link.txt 1.txt

lrwxrwxrwx 1 himanshu himanshu 5 Jun 21 12:45 1_link.txt -> 1.txt

-rw-rw-r-- 1 root root 0 Jun 21 12:12 1.txt

 

sanfoundry-> sudo chown -h root:users 1_link.txt

sanfoundry-> ls -l 1_link.txt 1

lrwxrwxrwx 1 root users 5 Jun 21 12:45 1_link.txt -> 1.txt

-rw-rw-r-- 1 root root 0 Jun 21 12:12 1.txt

 

12. Using chown to change the owner/group of a symbolic link directory recursively(chown -R -H …):

 

sanfoundry-> ln -s sample/ symbolic_folder

sanfoundry-> ls -l symbolic_folder

-rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:57 x

-rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:57 y

 

sanfoundry-> sudo chown -R root symbolic_folder

sanfoundry-> ls -l symbolic_folder

-rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:57 x

-rw-rw-r-- 1 himanshu himanshu 0 Jun 21 12:57 y

 

sanfoundry-> sudo chown -R -H root symbolic_folder

sanfoundry-> ls -l symbolic_folder

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:57 x

-rw-rw-r-- 1 root himanshu 0 Jun 21 12:57 y

 

Here as you can see that with -H option the owner name of the symbolic_folder is changed.

 

 

13. To change the permission for each file (chown -c file1 file2..):

 

sanfoundry-> ls -l 1.txt 2.txt

-rw-rw-r-- 1 root root 0 Jun 21 12:12 1.txt

-rw-rw-r-- 1 root himanshu 0 Jun 21 13:06 2.txt

 

sanfoundry-> sudo chown -c himanshu 1.txt 2.txt

changed ownership of `1.txt' from root to himanshu changed ownership of `2.txt' from root to himanshu

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.71 seconds
10,255,241 unique visits