Files

3.1 File Protection

Unix provides a mechanism for permitting access to files and directories by other users. For every file and directory in the file system, there are three classes of users who may have access:

  • User (u) The user is the owner of the file, usually the person who created it initially.
  • Group (g) All users are assigned one or more user groups. Therefore, there is also a group ownership associated with each file.
  • Other (o) All users other than the owner of the file or a member of the file’s group.
  • All (a) All users (User, Group, and Other). Each of these options is abbreviated to its first letter.

Let’s examine the output from “ls -alg”. The “-g” option causes “ls” to display the group ownership of a file. The “-a” and “-l” options cause all files to be displayed in long format.

% ls -alg
total 10
drwxr-xr-x 3 jsmith admin  512 Apr 5 16:29 .
drwxr-xr-x 4 jsmith admin  512 Apr 5 08:48 ..
-rw-r--r-- 1 jsmith admin 2315 Apr 5 16:29 mklogin.c
-rw-r--r-- 1 jsmith admin    0 Apr 516:29mt
-rw-r--r-- 1 jsmith admin 2053 Apr 5 16:14 test.c
% _

The information in the left-most column (in this example, d or -) tells whether the entry is a directory or a normal file. The next nine columns are divided into three groups of three characters, giving the permissions for the User, Group, and Others, respectively. The three permissions are:

  • Read (r) A user with read permission for a file can look at the contents of the file. If the file is a directory, then the user can find out what files are contained in that directory using the “ls” command.
  • Write (w) A user with write permission for a file can change its contents. Having write permission for a directory allows a user to create or delete files contained in that directory.
  • Execute (x) A user who has execute permission for a file can use that file as a Unix system command. Execute permission for a directory allows access to the files contained within the directory, and it allows one to move to that directory.

The next number in the output from “ls” is the link count for the file, with which you need not concern yourself. The user and group ownership of the file are given next, followed by the size of the file in characters. The last information given before the name of the file is the date and time at which the file was last modified.

3.2 Changing Permissions

To change the protection on a file, use the “chmod” command. The first argument to the “chmod” command is the protection mode. This is followed by the names of the files to be protected. The protection mode consists of three parts: which class of user to work with (u, g, o, or a), whether to grant (+) deny (-), or set (=) access, and which protection to use. Each of these parts is a single character. For example:

% chmod g+r *.tex chmod a-r resume chmod u+r resume 

to grant read access to your group for all files in your account ending in “.tex”. To prevent anyone (including yourself) from reading the file ‘resume’,  Since you own the file, however, you can give read access back to yourself

3.3 Viewing Files

So how do you look at a file? There are a host of programs that do that. One is “cat” which copies the contents of files to the terminal: “cat junk” types one file, and “cat junk temp” types out two. In the second example, the files are concatenated and displayed onto the terminal. If they are long files, they will probably scroll right off the screen.

The program “more” allows you to look at a file one screenful at a time. (To use “more”, type “more filename”.) Use the Spacebar to go to the next screen when you see:

More--(n%)

Pressing the Return key will display the next line. Press h to get a list of the other commands available in “more”. After you’ve become familiar with “more”, you may wish to look at the “less” program which does even more than “more”.

Scholarly Lite is a free theme, contributed to the Drupal Community by More than Themes.