chown and chgrp commands to change ownership of files and directories in Linux

The commands chown and chgrp are extensively used in linux systems to update the ownership and organize the file structure.

TL;DR:
The chown command can be implemented in the following two types of notations.
1) User and group owner Symbolic notation
2) Reference operator

The options/filters will be common to them for the two kinds on notations, We will look at the practical and important options.
The chown command syntax:

chown <username> <data-directory> <files>
chown <username>:<groupname> <data-directory>
chown --reference <Source-reference-file| Source-reference-Directory> <data-directory>

The ownership of the files is a privileged operation and it requires a sudo access to the system
Lets start by running the chown command on the files you want to change the ownership
We have a file called jdk-8u141-linux-x64.rpm and its owned by root user.

[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 root root 169980729 Jul 13 2017 jdk-8u141-linux-x64.rpm

We intend to change the ownership to user vamshi and we use chown command

[vamshi@linuxcent ~]$ sudo chown -v vamshi jdk-8u141-linux-x64.rpm 
changed ownership of ‘jdk-8u141-linux-x64.rpm’ from root to vamshi
[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 vamshi root 169980729 Jul 13  2017 jdk-8u141-linux-x64.rpm

By this only the user part of ownership was modified but the group was uneffected.

NOTE: We are using the option -v to print the command output information in verbose mode.

We can use chgrp command to modify the group ownership as follows:

[vamshi@linuxcent ~]$ sudo chgrp -v vamshi jdk-8u141-linux-x64.rpm 
changed group of ‘jdk-8u141-linux-x64.rpm’ from root to vamshi

To change both the user owner and group at once, chown command can be used, as demonstrated below:

[vamshi@linuxcent ~]$ sudo chown -v vamshi:vamshi jdk-8u141-linux-x64.rpm 
changed ownership of ‘jdk-8u141-linux-x64.rpm’ from root:root to vamshi:vamshi

[vamshi@linuxcent ~]$ ls -l jdk-8u141-linux-x64.rpm
-rw-r--r--. 1 vamshi vamshi 169980729 Jul 13 2017 jdk-8u141-linux-x64.rpm

How do you recursively change the permissions within the destination sub-directories and files ?

chown command when used with -R applies the updated ownership recursively till the directory depth on destination.

[vamshi@linuxcent data]$ ls -ld redmine/
drwxr-xr-x. 3 root root 17 Apr 17 16:31 redmine/
[vamshi@linuxcent data]$ sudo chown vamshi:vamshi /mnt/data/redmine/ -R
[vamshi@linuxcent data]$ ls -ld /mnt/data/redmine/
drwxrwxr-x. 3 vamshi vamshi 17 Apr 17 16:31 redmine-4.0.6

How to apply reference permissions to chown command in Linux ?

We shall be using the option --reference which takes the reference from the existing file and applies the permission

Suppose we directory called djangoproject1.
The root user copied the contents from elsewhere and placed  them in the location /home/alice .
Now the user alice has to have the user ownership to them in-order to work with djangoproject1.

[root@node02 alice]# ls -ld djangoproject1
drwxrwxr-x. 3 root root 108 Apr 16 11:45 djangoproject1

We now file called requirements.txt and the file is owner by the user alice and has the owner following permissions

[root@node02 alice]# ls -lth alice-requirements.txt
-rw-rw-r--. 1 alice alice 130 Apr 17 15:54 alice-requirements.txt

So we can simply reference this file and apply the same ownership permissions to djangoproject1, Lets see the demonstration.

[root@node02 alice]# sudo chown --reference alice-requirements.txt djangoproject1/ -R

As a result we have successfully modified the user and group ownership to alice using the --reference operator; Applying the same ownership permissions as our reference file alice-requirements.txt.

[root@node02 alice]# ls -lthr djangoproject1/*
-rw-rw-r--. 1 alice alice 405 Apr 16 11:44 djangoproject1/wsgi.py
-rw-rw-r--. 1 alice alice 756 Apr 16 11:44 djangoproject1/urls.py
-rw-rw-r--. 1 alice alice 3.1K Apr 16 11:44 djangoproject1/settings.py
-rw-rw-r--. 1 alice alice 0 Apr 16 11:44 djangoproject1/__init__.py
-rw-rw-r--. 1 alice alice 405 Apr 16 11:44 djangoproject1/asgi.py

djangoproject1/__pycache__:
total 8.0K
-rw-rw-r--. 1 alice alice 2.3K Apr 16 11:45 settings.cpython-36.pyc
-rw-rw-r--. 1 alice alice 159 Apr 16 11:45 __init__.cpython-36.pyc

Thus we can use the chown command to update the ownership information to files and directories and also use the chgrp command as per requirement.

Leave a Comment