Home > Unix > UNIX – Lesson 018 – chown and chgrp commands

UNIX – Lesson 018 – chown and chgrp commands


chown
The “chown” command is used to change the owner of a files and directories.

Permissions can only be changed by the owner (or root user, because he has full control over the attributes and access of files).

If you would like to give ownership of a file to another user on the system, you can ask to the system administrator to use the chown command.

Syntax :

chown [options] files/directories

Key argument :

  • -f    Force. Does not report errors.
  • -h   If the file is a symbolic link, this option changes the owner of the symbolic link. Without this option, only the owner of the file referenced by the symbolic link is changed.
  • -R   Recursive. chown descends through the directory, and any subdirectories, setting the specified ownership ID as it proceeds. When a symbolic link is encountered, the owner of the target file is changed, unless the -h option is specified.

Example

Starting from original permissions for “myfile” file equal to:

-rwxrwxrwx 1 xarabas users 22 Feb 20 14:53 myfile

If you run the following command:

$ chown hat myfile

the “hat” user became owner of “myfile” file instead of xarabas

Root can use chown to change, in the same time, the owner and the group of a file.

Running

chown hat:admins test test1

the “hat” user (with admins group) became owner of “test” and “test1” files files instead of xarabas (with users group)

================================================================================

chgrp
The “chgrp” command is used to change the group associated with files and directories.

Unlike the chown command, chgrp allows regular users to change groups, but only to one of which they are a member (otherwise root user, because he has full control over the attributes and access of files).

If you would like to give ownership of a file to another group on the system, you can ask to the system administrator to use the chgrp command.

chgrp newgroup files/directories

Syntax :

chgrp [options] files/directories

Key argument:

  • -f    Force. Does not report errors.
  • -h   If the file is a symbolic link, this option changes the group of the symbolic link. Without this option, only the group of the file referenced by the symbolic link is changed.
  • -R   Recursive. chgrp descends through the directory, and any subdirectories, setting the specified group ID as it proceeds. When a symbolic link is encountered, the group of the target file is changed, unless the -h option is specified.

Example

Starting from original permissions for “myfile” file equal to

-rwxrwxrwx 1 xarabas users 22 Feb 20 14:53 myfile

If you run the following command:

chgrp admins myfile

the group owner of “myfile” file became admins instead of users

If you run the following command:

chgrp users myfile

the group owner of “myfile” file became users instead of admins, we came back in the initial group settings.

Categories: Unix
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: