next up previous contents
Next: Copying files or directories Up: Files and directories Previous: Wild-cards and file name   Contents

File protection

Every file on Linux has a mode or protection. A file may be readable, writeable(deletable), and executable, in any combination. In addition, a file can be accessible to a single user (u), a group of users (g), or all other users (o). You are considered the owner of all files and subdirectories in your home directory. This means that you have total, unrestricted access to these files. Use the command ls -l to check the current protection settings for a file or a directory.

Consider the following example:

[amit@onyx]: ls -l program
-rw-r--r--   1 amit     faculty         0 Oct 25 13:15 program

There are ten protection bits. Assume that the bits are numbered 1 through 10 from left to right. Then bits 2,3 and 4 represent the protection for the user (or the owner). The bits 5,6 and 7 represent the protection settings for the group and the last three bits represent protection for others (not yourself or those in your group). Now we can read the above example. The file called program can be read by amit, anyone in the group faculty as well as any other user on the system. However only amit has write access to the file. The first bit has special meaning if it is set (see the man page for chmod for more on this special bit).

Consider another example:

[amit@onyx]: ls -l wideopen
-rwxrwxrwx   1 amit     faculty         0 Oct 25 13:23 wideopen

Everyone on the system has read, write and execute access to the file named wideopen. Suppose we want to remove write access from all users except the owner of the file. Then the owner of the file (amit) will use the following command.

 
[amit@onyx]: chmod g-w,o-w wideopen
[amit@onyx]: ls -l wideopen
-rwxr-xr-x   1 amit     faculty         0 Oct 25 13:23 wideopen

See the man page for chmod for more details.

Here is an example of protecting a directory from all other users.

 
[amit@onyx]: chmod g-rwx,o-rwx myhw
[amit@onyx]: ls -l myhw
drwxr------   1 amit     faculty         1024 Oct 25 13:23 myhw

To make a file executable by all users, use the chmod command:

chmod +x filename

This is useful for creating your own commands. See Section 4.7.1 for more on how to create your own commands.


next up previous contents
Next: Copying files or directories Up: Files and directories Previous: Wild-cards and file name   Contents
Amit Jain 2009-08-23