Previous Section  < Day Day Up >  Next Section

Recipe 9.4. Setting File and Directory Permissions with chmod's Symbolic Notation

9.4.1 Problem

You would like to change specific permission bits, rather than using the all-or-nothing approach of chmod's numeric notation, such as marking a script as executable.

9.4.2 Solution

The most common use for symbolic notation is to add the executable bit to a file's permissions without changing any other permissions:

$ chmod +x scriptname

The default action is a, or all, so the example makes scriptname executable by everyone. This adds the executable bit to the file owner only:

$ chmod u+x scriptname

You can surgically remove a specific mode bit. In this example, the group and other users lose their executable bits:

$ chmod go-x scriptname

This is a quick way to set the setgid bit on a directory, for creating a shared directory. All files created in this directory will have the same group ownership as the directory:

$ chmod +s /shared-directory

You can remove all permissions for group and other users by doing the following:

$ chmod go= scriptname

To make group permissions the same as the file owner's, use:

$ chmod g=u scriptname

9.4.3 Discussion

Using chmod's symbolic notation can get quite elaborate. This examples erases all existing permissions and starts over:

$ chmod -v a=,u=rwx,g=rx,o=r scriptname

You can do the same thing with chmod 754. Here's the key:

Symbolic notation is also called mnemonic notation:


r

Read


w

Write


x

Execute


X

File must already have execute permissions, or be a directory


s

Set user or group ID on execution—dangerous! do not use on executables, unless you really really know what you are doing!


t

Sticky bit


u

User, or file owner


g

Group file owner


o

Everyone else; others


+

Adds the new values to the existing values


=

Overwrites


-

Subtracts from existing values

9.4.4 See Also

  • info chmod

  • Recipe 9.7, for an explanation of sticky bits

  • Chapter 4 of LPI Linux Certification in a Nutshell, for exhaustive detail on permissions and ownership, right down to the binary level

    Previous Section  < Day Day Up >  Next Section