|Lesson 7||The chmod command |
|Objective||Use the chmod Command and its Options to change File Permissions. |
Use chmod Command and its Options to change File Permissions within the context of Unix System Administration
Permission bits are manipulated using the chmod command. This command has two basic forms. The first uses numeric codes:
$ ls -l filename
-rw-rw-r-- 1 jeremy jeremy 1145 Apr 8 09:40
$ chmod 755 filename
$ ls -l filename
-rwxr-xr-x 1 jeremy jeremy 1145 Apr 8 09:42
In the second form of the chmod command, permissions are added or removed by symbolic codes. The levels of user, group, and other are represented by the codes u, g, and o respectively. The permissions are represented as usual by r, w, and x.
The syntax is then
chmod [levels][+/-/=][permission] filename
chmod u+x filename
chmod og=r filename
chmod o-x filename
respectively give user execute permission for filename, set read permission only for other and for group, and remove execute permission for other. The = argument is designed to assign only a specific permission (or set of permissions) to a file or directory.
The = argument will revoke all permissions you do not specifically assign.
Here are a few more examples of the +, -, and = arguments.
In addition, the a code refers to everyone (all). To remove read permission from user, group, and other simultaneously, type
chmod a-r filename
The empty permission string can be used to remove all permissions from some levels. For example, the following command removes all group-level permissions:
chmod g= filename
The chmod command supports a recursive option. For example, to add read permissions for user, group, and other recursively on a directory and its subdirectories and files recursively , use
chmod -R a+r
Remember, too, that like other commands, chmod can be used with wildcards. To add read permission for user, group, and other to every file in the current directory, the following command will work well:
chmod a+r *
The chmod R command (and chmod itself) can have dangerous consequences.
For example, suppose you use chmod recursively while possessing root permissions. You would remove execute permission from practically every system command, including chmod. No commands could be run, so no easy fix would be available. Be careful, especially when working as root.
Chmod Change File Permissions