To change the owner and group owner of a file or directory, we use
chown (short for change owner). To change a new group, use the similar command
We must have root permissions to use this command.
The syntax for
chown is as follows:
$ chown [options] [owner]:[group] files
The syntax for
chgrp is very similar:
$ chgrp [options] newgroup files
Here are some functionalities of
chgrp that are used in the real world.
To change the file owner, specify the user, then the file name.
$ chown bob sampleFile.txt
The sampleFile.txt file will change user to bob.
To change the file's group owner, we set the group name after a colon (
:). This provides the same function as
$ chown :programmers todo-list.txt $ chgrp programmers todo-list.txt
$ chown bob:redditors sampleFile.txt
Here, we are changing the sampleFile.txt. The argument before the : indicates the new user, while the argument after shows the new group.
To recursively grant ownership of a directory and all files with subdirectories within, use the
$ chown -R bob /files/folder
Let's go through an example with two users: marie and bob.
marie has access to sudo commands, and wants to copy a file into bob's home directory. She would like to give bob the ownership.
$ sudo cp sampleFile.txt ~bob Password: # Copied file to bob's homepage. $ sudo ls -l ~bob/sampleFile.txt -rw-r--r-- 1 marie staff 2014-01-13 16:00 /home/bob/sampleFile.txt # bob only has read access to this file. $ sudo chown bob: ~bob/sampleFile.txt -rw-r--r-- 1 bob bob 2014-01-13 16:00 /home/bob/sampleFile.txt # Now bob has ownership and has write access.
Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.$ Check price
Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.$ Check price