Changing the Group Association of Files and Directories with chgrp
Suppose you have a file called black that is currently being used by the pot group and you want to change the file's permissions so that it can be accessed by the kettle group. To do this, you'll need to change which group the file is associated within this case, change the association from the pot group to the kettle group. You can change which group a file or directory is associated with using chgrp, as shown in Code Listing 5.5.
Code Listing 5.5. Pots and kettles can both be black, but only one at a time.
[ejr@hobbes permissions]$ ls -l total 178 -rw-rw-r- 1 ejr pot 24850 Jul 24 14:59 black -rw-rw-r- 1 ejr ejr 128889 Jul 24 14:33 sage.sayings -rw-rw-r- 1 ejr ejr 23890 Jul 24 14:33 sayings [ejr@hobbes permissions]$ chgrp kettle black [ejr@hobbes permissions]$ ls -l total 178 -rw-rw-r- 1 ejr kettle 24850 Jul 24 14:59 black -rw-rw-r- 1 ejr ejr 128889 Jul 24 14:33 sage.sayings -rw-rw-r- 1 ejr ejr 23890 Jul 24 14:33 sayings
To change group association with chgrp:
Type ls -l at the shell prompt to verify the file's name and the group it's associated with. Remember that the second column in the middle of the listing, just before the file sizes, lists the group membership.
chgrp kettle black
Type chgrp followed by the name of the new group you want the file to be associated with and the filename. Here, the chgrp command changes the group association for the file called black to the kettle group.
If you try to change group ownership and get an error message like "Not owner" or something similarly obscure, your userid doesn't have the necessary authority to make the change. You'll have to ask your system administrator for help.
Change group association only if you have a specific need to do so; you don't want to make your files available to other people unnecessarily. Unless you are the system administrator, you won't be able to control exactly who belongs to the group to which you've given access to your files.
If you change the group association of a specific directory, you also need to check permissions for the directory containing it. Users will not be able to change into the specific directory (regardless of their group membership) unless they also have read and execute permission for the directory containing it.
Just as with the cp and mv commands, covered in Chapter 2, you can use a -R flag with chgrp to recursively apply changes to a directory and all of the subdirectories and files in it. For example, to change the group association of the LatestProject directory and all its contents to the project group, use chgrp -R project LatestProject from the directory above LatestProject.
After you change a file's ownership, what you can do with the file depends on the group and other permissions and memberships. The new owner, however, will be able to do anything with the file.
When changing the ownership of a directory, you can add the -R flag to chown to make it apply recursively to all files and directories below it.
If the system does not allow you to use chown to give files away, consider using cp to make a copy of a file to accomplish the same thing. If you copy someone else's file (that you have permission to read) to another name or location, the copy is fully yours. (In this giving-the-file-away example, the recipient should use cp.)
Even if you can't use chown, you could still be able to request that the system administrator change file ownership for you: "Could you please change the ownership of my rowyourboat file to Merrilee, with chown merrilee /home/shared/me/rowyourboat. Thanks!"(This could happen because many versions of Unix don't allow non-root users to change file ownership.)