Go to the previous, next section.

Protecting Files and Directories

There are two ways to change a file's permissions. The first way is to set the permissions with a text string. Say we wish to make a file owner=read, group=read, and other=read. To do this we would do

chmod ugo=r foo

where u=owner, g=group, and o=other, and foo is the filename. Now doing an ls -l, will produce the following output:

-r--r--r--  1 tot       2437 Sep  8 15:59 foo

Alternatively, (from `foo''s current state), we could do:

chmod u+wx foo

where the `+' means add to the current permissions, produces:

-rwxr--r--  1 tot        2437 Sep  8 15:59 foo

This method should be obvious by now. The second method uses three sets of binary numbers, converted to decimal. So, if you wish to give owner read, write, group permission and all others read permission, enter:

chmod 644 foo

where the first digit is for the owner, second is for the group, and third is for all other, would produce:

-rw-r--r--  1 tot        2437 Sep  8 15:59 foo

Notice the 644 is easily extracted from `-rw-r--r--'.

                        6        4       4
                        |        |       |
                      4 + 2      |       |
                       \  |      |       |
                        \ |      |       |
                         421     421     421
                         rw-     r--     r--

All you have to do is look and see which bits are turned on, add them up and, poof, you have your number to call chmod with. Say you wish read, write, and execute (rwx) on owner, read and execute on group and all others (r-x), enter:

chmod 755 foo

-rwxr-xr-x  1 tot        2437 Sep  8 15:59 foo

Figure shows how you can obtain 755 from `-rwxr-xr-x'.

                          7       5       5
                          |       |       |
                      4 + 2 + 1  4+1     4+1
                       \  |  /   | |     | |
                        \ |/     | |     | |
                         421     421     421
                         rwx     r-x     r-x

You may take your pick of whichever method you prefer. Directory permissions work the same way, except what it means to execute a directory is basically permissions to list the contents of it. Hence, a directory of:

drwxr-xr-x  2 tot         512 Sep  8 17:46 foo

(notice the `d' on the far left, that means it's a directory)

this means the owner tot, may read, write & execute it (cd into it and list it's contents). It is uncommon to only give a user read privilege to a directory and not execute, although you may. This would allow one to read from your directory but not list it's contents.

One more thing, if you wish all files to be created with some certain permissions, use umask. For example, if you issue umask 022 you will mask all your upcoming created files with 022, thus producing a newly created file with permissions (644):

-rw-r--r--  1 tot        2437 Sep  8 18:12 foo

If you do a man on umask (man umask) it will explain in more detail about file masking.

Go to the previous, next section.