How to change the ownership of a file/directory – Unix/Linux

The owners of a file/dir under Linux are lisred below:
1, Owner
2, Group
3, Others

You can change the ownership of a file/dir by using the ‘chown’ command.
Syntax:

# chown newowner file/dir 

Example:

[[email protected] ~]# touch crybit
[[email protected] ~]# ll crybit
-rw-r--r--. 1 root root 0 Feb  1 05:01 crybit
-----
[[email protected] ~]# chown crybit crybit
[[email protected] ~]# ll crybit
-rw-r--r--. 1 crybit root 0 Feb  1 05:01 crybit

Changed the ownership from root to user crybit.

How to change the ownership recursively?
Use the switch ‘R’ to do the ownership change recursively.
Example:

[[email protected] ~]# mkdir dir
[[email protected] ~]# cd dir/
[[email protected] dir]# touch a{1..5}.txt
[[email protected] dir]# ll
total 0
-rw-r--r--. 1 root root 0 Feb  1 05:09 a1.txt
-rw-r--r--. 1 root root 0 Feb  1 05:09 a2.txt
-rw-r--r--. 1 root root 0 Feb  1 05:09 a3.txt
-rw-r--r--. 1 root root 0 Feb  1 05:09 a4.txt
-rw-r--r--. 1 root root 0 Feb  1 05:09 a5.txt

Change the ownership recursively by using the switch ‘R’

[[email protected] dir]# cd ..
[[email protected] ~]# chown -R crybit dir/
[[email protected] ~]# cd -
/root/dir
[[email protected] dir]# ll
total 0
-rw-r--r--. 1 crybit root 0 Feb  1 05:09 a1.txt
-rw-r--r--. 1 crybit root 0 Feb  1 05:09 a2.txt
-rw-r--r--. 1 crybit root 0 Feb  1 05:09 a3.txt
-rw-r--r--. 1 crybit root 0 Feb  1 05:09 a4.txt
-rw-r--r--. 1 crybit root 0 Feb  1 05:09 a5.txt

That’s it!! 🙂

Related Links:
chgrp, groupdel, groupmems, groupmod, useradd , usermod .
More
ls, head, tail, top, ps, find, crontab

Post navigation

Arunlal Ashok

Operations Engineer at Endurance International Group. Linux lover. Like to play on Linux console. I started this blog to share and discuss Linux thoughts.

Always happy for an open discussion! Write to arun (@) crybit (dot) com. Check about me for more details. About this blog and our strong members, check The team CryBit.com
We like to travel. Our travelogues are published at Trip Mentor

5 thoughts on “How to change the ownership of a file/directory – Unix/Linux

Leave a Reply

Your email address will not be published. Required fields are marked *