How to handle unix files
Table of Contents

  The Directory Tree
Path Names and Working Directories
Home Directories and Quotas
File Access Rights
More About "ls"

Related Links

  Top 10 UNIX Commands


THE DIRECTORY TREE

All Unix disk files are stored in one directory tree. This includes both system files and user files. The files are grouped in directories, which are simply collections of files and/or more directories. The directory tree is spread across several disk drives; this is not obvious to the user, and is generally important only to systems personnel.

Access to some parts of the directory tree is restricted by userid, group, or by login server, but most of the tree structure will be the same whether you log in to one of the primary Computer Centre Unix login servers (including mira, antares and deneb), or to one of the open area Unix workstations.

Some computers in the Unix system act as Network File System (NFS) servers. Their main task is to control the disk packs and transmit files to and from the user's host computer. Automount software makes sure that all the essential files are available from any Computer Centre Unix machine.

As system load changes or new machines are added to the network, the functions of various servers will change. Such changes at the system level do not require the user to make any changes.

In Unix, you do not define file attributes such as record length or block size. Any file is just a string of data bytes. A "newline" character is used by software to recognize record boundaries. Newline is usually denoted as , in hexadecimal it is 0a.

The directory structure is represented as a tree, usually drawn upside down. The root or main directory, which contains all other directories and files, is represented in Unix by the character "/". Thus, the directory "home" (a major branch of this tree) would be denoted by "/home".

Much simplified, the top levels of the tree look something like this:

The naming convention in Unix is that a "bin" directory will contain binary (executable) files; "tmp" will contain temporary files; "lib" will contain software libraries; and "man" will contain manual pages.

The directory /home contains the home directories of everyone with a Computer Centre Unix (CCU) userid. Your home directory is the portion of the file structure which is completely your own to control. Because it contains so many directories, /home is split up into smaller subdirectories, including u1 through u14.

The directories in /usr contain software for user applications. These include the executable files for commands such as "cat" and "pine". Directory /usr/lib contains libraries used by vendor software. Directory /usr/local contains locally installed software, and /usr/local/doc contains local documentation.

Each login server has its own copies of the directories called /tmp and /var/tmp, which are available for storing temporary files. If you create /tmp/mine while logged in to mira, you will not be able to read the file when you are logged in to an open area Sparcstation. The directory /tmp is cleared daily and /var/tmp is cleared weekly. /tmp is also cleared any time that the machine is rebooted.

The directory /usr/local/tmp is shared across all the ACN Unix login servers, so you don't have to log in to the same login server to access a file previously saved there. All files in this directory are removed early each Monday morning.

Files in all temporary file systems may be removed if the file system is filling up.

For the Curious

You can find out more about directories using the df command. For example:

    df -k /var/tmp ;df -k /usr/local/doc  

will show something like the following:

    Filesystem        kbytes   used    avail   capacity  Mounted on   /dev/dsk/c0t3d0s4 283839 132557   122899      52%    /var  Filesystem        kbytes   used    avail   capacity  Mounted on   sirius:/x/share   917862 541663   824413      66%    /local/share   

This shows you that /var/tmp is on a file system local to your login server, but that /usr/local/doc is made available through the NFS server sirius, and is actually stored in the /local/share directory.

The Unix file structure is quite complicated when you look at it closely, as branches of the tree can be interconnected by symbolic links. Thus, /usr/local/doc becomes an alias for /local/share/doc. Besides the df command above, if your login shell is csh or tsch, you can see evidence of this branching using:

    cd /usr/local/doc;pwd  

which changes your working directory to /usr/local/doc, then prints your working directory as /local/share/doc. If that isn't confusing enough, you can try:

    cd /usr/local;pwd  

If you expected to see either "/usr/local" or "/local/share", guess again. You're really looking at "/opt/local". What appears at first glance to be a simple tree structure is more like a tangled vine.

Evidence of links in the file system can also be found using the ls command:

    cd /usr/local; ls -l doc  

This shows the link as "doc -> share/doc".

Also, because of the way the Network File System works, some directories may not be visible to the ls command if no one has tried to access them from a particular login server.


PATH NAMES AND WORKING DIRECTORIES

File names in Unix are specified either by relative path name, that is, their position relative to the user's current working directory, or by full path name. File names can be any length and contain any characters except "/" and the null character. However some characters should only be used with care (or not at all) such as the blank space and the dash ('-').

When you log in, your home directory is your working directory. You change your directory every time you use a cd command. Let's take the userid "opersci" as an example. When the advisor logs in, his working directory (displayed by a pwd command) is /home/u1/opersci. If he enters:

    cd guide  

then his working directory becomes /home/u1/opersci/guide. If there is a file called "emacs" in the guide directory, you would be able to read (but not change) this file using the command:

    more emacs  

The user can then return to their home directory, using either:

    cd ..  

to simply back up one level, or:

    cd  

which always takes you to your home directory wherever you are in the file system. The user can still read the "emacs" file by specifying where to find it (relative to their current location):

    more guide/emacs  

However, if the user wants to read the file "Changes" in the "elm" directory under "/usr/local", they must use the full path name for the file they wishes to read:

    more /usr/local/src/elm/elm.2.3/Changes  

Similarly, to read a file belonging to another user, you must use a path name that includes that user's home directory, for example:

    cat /home/u5/support/short  

However, some Unix shells, including the K shell which is the default here, allow an abbreviation for a users' home directory, so the command above could be shortened to:

    cat ~support/short  

This is known as "tilde expansion".

You can use the cd command for any directory that does not have access restrictions. For example, you can make ROOT your working directory using:

    cd /  

If you then enter the command:

    ls -F  

you will see what other files and directories exist at the root. The -F (Format) on the ls (list) command will cause directories to be marked with the character "/", executable files to be marked with "*", and symbolic links to be marked with "@". A symbolic link means that the directory is not really stored at this position in the tree structure. It is a pointer to some other directory. An example of this is /usr/tmp. If you try:

    cd /usr/tmp;pwd  

you will see that you are really located to /var/tmp. The name /usr/tmp is just an alias, set up because it is a better name for temporary user files.


HOME DIRECTORIES AND QUOTAS

Part of the reason for setting directories aside for temporary user files is that each user has a quota on the amount of disk space their home directory can occupy. Unless you've been able to make a very good case for an increase, you will have a "soft" quota of 25 megabytes and a "hard" quota of 30 megabytes. You can't make your disk file allocation exceed the hard quota, but you can exceed the soft quota temporarily. The quota command is used to display your quota information:

    quota  

The quota command will display your current disk space usage, your soft quota, and your limit (hard quota). If you exceed the soft quota, you will receive periodic reminders to reduce your allocation below the soft quota limit. After that, you cannot create any new files until the excess allocation is freed.

You can use the tmp directories for short term storage of files that would cause you to exceed your quota. However, keep in mind that the disk volumes that hold the user home directories are backed up every night, so that you wouldn't normally lose more than one day's work if those files are damaged. The tmp directories are not backed up.

Another strategy for reducing problems with your disk quota is to use the zip and unzip commands to store the files that you don't use regularly in a compressed format. If you keep only the zipped copy in your own directory space, unzipping to a directory in /local/tmp to work with the files, then zipping back into the original location in your own directory space when you are finished, you can improve your data storage capacity significantly.

For more information on coping with Unix disk quotas, please see:
How to Manage Unix Disk Space and Quotas.


FILE ACCESS RIGHTS
Selecting File Protection Defaults

All files you create can be read, written and executed only by your own userid, unless you actively change the access rights. (If your userid was issued before 1990, this may not be the case. Check your .login file for a "umask" command. If it says "umask 022" then your files can be searched and read by anyone, unless you take action to prevent this.) You can use the umask command to display or set the initial protection mode of new files and directories. You can check its current value using the command:

    umask  

The usual setting is 77 which means that the owner has complete access, and everyone else has no access.

If you supply a three digit value on the command, the umask setting is changed for the rest of the session, or until another umask command is issued. The three digits are codes for the access rights of the owner, the owner's group, and other users. A zero gives no protection, a 1 gives search protection, a 2 gives write protection and a 4 gives read protection. These values are added together to select the umask digits. Don't use 777 unless you want to exclude yourself from your files! To make permanent changes to your umask, edit the command in your .login file.

The umask definition is set up so that execute permission on files is not automatic. You can use umask to give default "execute" permission on directories; directories aren't executable, so this is used for search permission.

Search permission includes the ability to cd to a directory. However, to use the ls command you must also have read permission for the directory.

As an example, if you want to keep complete file access for yourself, and also allow your group to search your directories and read your files, you would set:

    umask 027  

Now, when you create a new file, people outside your group can't read it, write to it, or execute it, and people in your group can't write to it.

Groups

By now, you are probably wondering: "What group are they talking about?"

All Unix users are members of one or more groups. These groups are used to define a list of people who should have similar access rights to various parts of the Unix file system. Normally, all members of one administrative unit would belong to the same group. Students would belong to group "student".

To find out what group or groups you belong to, enter:

    groups  

Each file or directory also has a group associated with it, which will be displayed if you use -l on the ls command. You can change the group attribute for a file or directory using the chgrp command.

Changing Access Rights on Individual Files

The -l option on the ls command will display the access rights associated with files and directories. For example, if the working directory is the opersci home directory, then:

    ls -l  

could produce a display that includes:

    -rw-rw-rw-  2 opersci  cserv    156 May 16  1992 public  -rwxrw-rw-  2 opersci  cserv    156 May 16  1992 status-report  drwxrwx---  2 opersci  advisor  512 May 15  1992 src  drwx------  2 opersci  advisor  512 Jun  1  1990 textformat  

The first character of each line is "d" for directories or "-" for files. The next three characters give the access rights of the owner; in this case, read and write on all files, and execute on status-report. In the two directory descriptions, the "x" indicates the right to search the directory. The following three characters give the access rights of the group (in the same order - read write execute). The last three characters give the access rights to the world or others (i.e. all other users).

Only the userid given as the owner of the file has the ability to change the access permissions of a file.

Files and subdirectories in Unix do not inherit the access rights of their parent directories. In the given example, the group permission "rwx" for directory "src" does not necessarily mean that users in the group "advisor" have complete access to every file in the directory. The access rights can be defined separately for every file and subdirectory. Also, in order to have access to a particular file, you must have, search permission for all of the parent directories.

The chmod (change mode) command is used to change access rights on existing files and directories.

For example, to prevent "other" users from making changes to the file "public", opersci could enter:

    chmod o-w public  

This causes the change (subtract write permission from others) to be made to the file or directory named public.

To add group read and search permission to the directory textformat and its contents, opersci could enter:

    chmod -R g+rX textformat  

The -R option makes the command recursive; that is, it will be applied to all files and directories included in textformat.

The g+rX part of the command above is called a symbolic mode. The first part specifies the user class, this can be u for user, g for group or o for others. The operation can be + for add, - for subtract, or = for assignment. The permission can be r for read, w for write, or x for execute.

If X is a capital letter, it means search directories and execute files where some other user class has execute permission. In other words, execute permission is granted only where it is relevant.

There are other permissions possible, and you can also code the mode in octal; see the manual page chmod for an explanation:

    man chmod  

MORE ABOUT "ls"

Most Unix userids have been set up so that the command:

    ls -l  

(list in long format) can be abbreviated to ll and the command:

    ls -CF  

(list in columns with formatting) can be abbreviated to lsf.

To see what aliases are currently in effect for your userid, enter:

    alias  

The commands that have been used to set these aliases can be found in a "dotfile" in your home directory. For C shell users, this file is named ".cshrc"; for Korn shell users, it is ".kshrc".

The "dotfiles" such as .login and .cshrc (C-shell), .profile and .kshrc are called hidden files, and as such they are not normally displayed when using the list command (ls). To see the "dotfiles" you must use the -a (all files) option. Using the command:

    ls -aF  

will not only display the hidden files, but two special files as well: "./" and "../". These represent the current directory (".") and parent directory (".."), respectively.


IST SERVICE DESK

Mon - Fri: 8am to 8pm
204-474-8600 or
Chat Now button with link

Walk-In:
Fort Garry
123 Fletcher Argue
Mon - Fri: 8am to 8pm

Bannatyne
230 Neil John Maclean Library
Mon - Fri: 8am to 4:30pm

New! Submit requests & check ticket status online at: umanitoba.ca/ist/service_catalogue/