1. Introduction

In Linux, all the files and directories, have owners and groups by default.

In this tutorial, we’ll discuss different ways to change the group ownership of directories and files using the chgrp command.

2. Group Ownership for Files

In this example, we’re going to change the group ownership of a file.

Let’s start by listing the files with ls command that will support our example:

$ ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 5:30 file1
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file2
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 9:30 file3
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file4
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 29 12:28 letters

As we can see, all our files are under the ownership of the group ubuntu.

Let’s see how we can change the group of a file.

First, we need to create our new group. Depending on our user privileges, we may need to use sudo:

$ sudo addgroup rachel

Now, we can add file1 to the group rachel:

$ sudo chgrp rachel file1
$ ls -l
total 4
-rw-rw-r-- 1 ubuntu rachel    0 Apr 29 5:30 file1
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file2
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 9:30 file3
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file4
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 29 12:28 letters

3. Group Ownership for Directories

To change the group ownership of a directory, we can also make use of the chgrp command.

Again, let’s start by listing the directories that support our example:

$ ls -l
drwxrwxr-x 3 ubuntu ubuntu 4096 Apr 29 10:30 mydirectory
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 29 10:43 mymovies
drwxrwxr-x 4 ubuntu ubuntu 4096 Apr 29 16:28 mytrips

Now we change the group of mydirectory to rachel:

$ sudo chgrp rachel ./mydirectory

$ ls -l
drwxrwxr-x 3 ubuntu rachel 4096 Apr 29 10:30 mydirectory
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 29 10:43 mymovies
drwxrwxr-x 4 ubuntu ubuntu 4096 Apr 29 16:28 mytrips

It’s worth noting that the group of the files inside mydirectory were left untouched. We can use -R to affect the files as well.

4. Symbolic Links

ln is a command-line in Linux that creates links between files. We start by creating a symbolic link between file1 and file1_link.

$ ln -s file1 file1_link
$ ls -l
total 2
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file1
lrwxrwxrwx 1 ubuntu ubuntu    5 May 15 17:27 file1_link -> file1

To change the group ownership of the file linked by our symbolic link, we must use the –dereference option so that it only affects the reference of our symbolic link:

$ sudo chgrp --dereference rachel file1_link

$ ls -l
total 2
-rw-rw-r-- 1 ubuntu rachel 0 Apr 29 10:30 file1
lrwxrwxrwx 1 ubuntu ubuntu 5 May 15 17:27 file1_link -> file1

In case we want to modify the symbolic link itself we use the –no-dereference option:

$ sudo chgrp --no-dereference rachel file1_link
total 2
-rw-rw-r-- 1 ubuntu ubuntu 0 Apr 29 10:30 file1
lrwxrwxrwx 1 ubuntu rachel 5 May 15 17:27 file1_link -> file1

5. Subdirectories

We’ve now decided to change all our files inside the directory named mydirectory under the group rachel.

To get to that we must use the -R (recursive) flag:

$ sudo chgrp -R rachel ./mydirectory/
$ ls -l
total 4
-rw-rw-r-- 1 ubuntu rachel    0 Apr 29 10:30 file1
-rw-rw-r-- 1 ubuntu rachel    0 Apr 29 10:30 file2
-rw-rw-r-- 1 ubuntu rachel    0 Apr 29 10:30 file3
lrwxrwxrwx 1 ubuntu rachel    5 May 15 17:27 file_link -> file1
drwxrwxr-x 2 ubuntu rachel 4096 Apr 29 12:28 letters

6. Verbose Output

In case we want to see more information about the changes, we can use the -v (verbose) flag:

$ sudo chgrp -R -v rachel ./mydirectory/
group of './mydirectory/file_link' retained as rachel
group of './mydirectory/file3' retained as rachel
group of './mydirectory/file2' retained as rachel
group of './mydirectory/file1' retained as rachel
group of './mydirectory/letters/readme' retained as rachel
group of './mydirectory/letters/magazines' retained as rachel
group of './mydirectory/letters/books' retained as rachel
group of './mydirectory/letters' retained as rachel
group of './mydirectory/' retained as rachel

7. Conclusion

In this tutorial, we learned how to manipulate groups of files, directories, and symbolic links using the chgrp command.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments