CHOWN command explained with examples

This is our second post in “Linux Files and folders” Series and first one is about chmod command in detail. chown is a command to change the ownership of a file/folder or even multiple files/folders at a time to a specified user/group. CHOWN stands for CHange file OWNer and Group.

CHOWN syntax

you can use man command to get full details about chown command.

man chown

chown options user:group file/folder

In this post we will see some simple and complex examples which is required for every Linux system admin. I strongly believe learning through examples is good way to understand many tings.

Examples :

Exampl1: How can I make mark as the owner of the file?

$ chown mark file1

The above command the group owner will not be changed.

Example2:How can I change ownership of multiple files to jim user?

$ chown jim /path/to/file1 /path/to/file2 /path/to/file3

or

$ chown jim /path/to/{file1, file2, file3}

Example3:How can I change the owner and group to zen and sales group for a file.

$ chown zen.sales file1

or

$ chown zen:sales file1

Note: . And : are interchangeable, so you can use anything.

Example4: How about to change owner and group permissions where I know only their UID and GID?.

Use those UID and GID in place of new owner and group

$ chown 625:874 file1

Here user UID is 625 and GID of the new group is 874.

Example5: How to change ownership to tim and group to user’s(tim) primary group.

$ chown tim. File1

Note:Here you did not given group name after ., so chown command will take primary group of the tim user.

Example6:How to change only group permissions to group: techsupport?

$ chown .techsupport file1

Example7:how to change a folder and its content to mark ownership/group?

$chown -R mark:sales /path/to/directory

Example8:how to change the ownership permissions forcefully/silent/quiet and don’t print any error?

$chown -f mark /path/to/file1

Note: We can mix up -R and -f options when ever required.

Tricks/Ideas when dealing with scripting and automating tasks

Example9: I have a file located in /home/user1/testfile, now my requirement is I want the same ownership of this file to my second file which is located in /var/ftp/file2, how can I do it with out knowing the owner of that file?

Use reference option for this as shown below..

$chown –reference=/home/user1/testfile /var/ftp/file2

Example10: How about changing all the files owned by user:Terry in /var folder with new employee:Rob joined as replacement of him?. Use –from option on that folder as shown below.

$ chown –from=terry rob /var/*

Some rarely used options..

c –Show verbose output only when a change is made.

v – Show verbose output for every file processed.

Caution:

1) Be care full when using chown command. See below two examples..

chown user1:group1 / var/ftp

chown user1:group1 /var/ftp

Any difference you find between two commands?, there is slight difference if you see first command there is a space between / and var/ftp. This is very dangerous mistake which will change entire file-system owner to user1.

2) Some times there will be user called “nobody” as the owner of some files(This is caused when an user account is deleted with out deleting his files/folders), at this time chown command will behave differently.user deluser command when deleting a user so that these type of files will not be created or use find command to find all these type of files and change the ownership to new owner

3) Some time you will “Operation not permitted” and “Permission Denied”, at this time please check if you are the owner of the file or not?

4) Please add your tips and tricks in the comments section so that I will will add up to this list ;)