Using Directory Permissions

Unlike many other operating systems, UNIX stores the contents of directories in ordinary files. These files are similar to other files, but they are specially marked so that they can only be modified by the operating system.

As with other files, directories have a full complement of security attributes: owner, group, and permission bits. But because directories are interpreted in a special way by the filesystem, the permission bits have special meanings (see Table 5.11).

Permissions for Directories
Contents Permission Meaning
r read You can use the opendir() and readdir() functions (or thels command) to find out which files are in the directory.
w write You can add, rename, or remove entries in that directory.
x execute You can stat the contents of a directory (e.g., you can determine the owners and the lengths of the files in the directory). You also need execute access to a directory to make that directory your current directory or to open files inside the directory (or in any of the directory's subdirectories).

If you want to prevent other users from reading the contents of your files, you have two choices:

  1. You can set the permission of each file to 0600, so only you have read/write access.
  2. You can put the files in a directory and set the permission of that directory to 0700, which prevents other users from accessing the files in the directory (or in any of the directory's subdirectories) unless there is a link to the file from somewhere else.

Note the following:

Removing Funny Files

One of the most commonly asked questions by new UNIX users is "How do I delete a file whose name begins with a dash? If I type rm -foo the rm command treats the filename as an option." There are two simple ways to delete such a file. The first is to use a relative pathname:

% rm ./-foo %

A second way is to supply an empty option argument, although this does not work under every version of UNIX:

% rm - -foo %

If you have a file that has control characters in it, you can use rm command with the -i option and an asterisk, which gives you the option of removing each file in the directory - even the ones that you can't type.

% rm -i * rm: remove faq.html (y/n)? n rm: remove foo (y/n)? y %

A great way to discover files with control characters in them is to use the -q option to the UNIX ls command. You can, for example, alias the ls command to be ls -q. Files that have control characters in their filenames will then appear with question marks:

% alias ls ls -q % ls f* faq.html fmMacros fmdictionary fo?o faxmenu.sea.hqx fmMacrosLog.backup fmfilesvisited % 

Table 5.12 contains some common directory permissions and their uses.

Common Directory Permissions
Octal Number Directory Permission
/ Anybody can view the contents of the directory, but only the owner or superuser can make changes.
/tmp Any user can create a file in the directory, but a user cannot delete another user's files.
$HOME A user can access the contents of his home directory, but nobody else can.