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:

[root@localhost ~]# touch crybit
[root@localhost ~]# ll crybit
-rw-r--r--. 1 root root 0 Feb  1 05:01 crybit
-----
[root@localhost ~]# chown crybit crybit
[root@localhost ~]# 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:

[root@localhost ~]# mkdir dir
[root@localhost ~]# cd dir/
[root@localhost dir]# touch a{1..5}.txt
[root@localhost 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’

[root@localhost dir]# cd ..
[root@localhost ~]# chown -R crybit dir/
[root@localhost ~]# cd -
/root/dir
[root@localhost 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

Arunlal Ashok

Sr. Linux Server Administrator. I'm managing Linux servers since 2012. I started this blog to share and discuss my ideas. Check My Profile in uPwork (oDesk) and create a job, if you need any administration help. Thanks!!

You may also like...

3 Responses

  1. February 1, 2014

    […] Links: chown, groupdel, groupmems, groupmod, useradd , usermod […]

  2. February 1, 2014

    […] More: ls, head, tail, top, ps, find, crontab, groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown […]

  3. February 5, 2014

    […] commands, groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, […]

Leave a Reply

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