Main index

Introducing UNIX and Linux


More on shells

Overview
Simple arithmetic
      Arithmetic expansion
            Operators for arithmetic expansion
      The 'expr' command
Pattern matching
      Patterns
            Examples of patterns
      The case statement
Entering and leaving the shell
More about scripts with options
Symbolic links
Setting up terminals
Conventions used in UNIX file systems
Summary
Exercises

Conventions used in UNIX file systems

In this section we look in more detail at which files are stored where. Although your home directory will be located at least one level down the hierarchy, and whatever subdirectories you create are your own business, there are some conventions it would be unwise to ignore. Conventions are not always followed, however, even in parts of the file hierarchy that contain system files, and there is no requirement for them in the standards.

Executable files, whether they are binary files or executable shell scripts, are usually held in directories called bin, or with bin as part of their name. For instance:

/home/ugrad/chris/bin
/home/ugrad/chris/import/bin
/home/ugrad/chris/bin/star4
/home/ugrad/chris/bin/scripts

If bin is the last component of the pathname, the previous components would typically indicate some property of the commands held in the directory. The directory /home/ugrad/chris/import/bin might well hold commands chris has been mailed by colleagues. 'Imported' is used to indicate commands have come from other systems. If bin is not the last component, subsequent names in the pathname might indicate the type of machine the commands can run on. Commands in /home/ugrad/chris/bin/star4 might be binary commands (recall that binary code is machine-specific) that will only run on a Star4 system, and /home/ugrad/chris/bin/scripts might contain shell scripts.

Devices are contained in directories called dev; most systems will simply have /dev as the only such directory, since they cannot be created by users at will. Manual pages are always contained in a hierarchy with man as the last component. Source code (such as C or Pascal programs) is often held in directories called src. Files that must be stored temporarily while being sent to a printer or waiting to be sent off by the electronic mail program, are held in directories called spool. Files and directories whose size is known to vary considerably are often held in a directory called var. It would not be uncommon for chris's mailbox - the file in which incoming mail is stored before being read - to be the file /var/spool/mail/chris. Libraries - that is, sets of data required by specific utilities such as the C compiler - are held in directories called lib, and 'include' files - also required by the C compiler - are held in directories called include. Have a look at the root directory, and you will see several of these directories.

There is a wide variety of practice across manufacturers and institutions, but these conventions are broadly adhered to, even if minor variations are added; if you find a directory called 4lib you would be fairly safe guessing it to be a 'library' directory.

The last directory name that interests us here is tmp. This directory is used for temporary files. Many commands - including several of the scripts in this book - use temporary files that are only required while the command is running, and the existence of these files is of no interest to the user running the command. Instead of using the current directory to store the temporary files, it is good practice to use a completely different directory. There are two principal reasons for this. First, it avoids the problem of the current directory filling up with unwanted files (should they accidentally not be deleted) and secondly, it prevents existing files being overwritten (should their names happen to coincide with that of the temporary file). There is also an advantage from the viewpoint of the system administrator - provided that the locations of the tmp directories are known, they can periodically have their contents removed, so that unwanted temporary files do not waste storage space.

You can expect to find a directory called /tmp, and you can choose names for temporary files to place in that directory by using $$ (the current process number) as part of the filename.

Worked example

Write a script to repeatedly request you to type in the names of files, and to concatenate them and display on the terminal the resulting file after all the concatenation has taken place. The script should terminate when you enter a blank line in response to the request for a filename.
Solution: We need to concatenate the files to a temporary file, cat that file, then delete it.

# Start by choosing a unique name for the temporary file
TMPFILE=/tmp/$LOGNAME.$$

# Double check that it doesn't exist - just in case
if   [ -f $TMPFILE ]
then echo "Temporary file exists"
     exit 1      # The command fails ...
fi

while true       # Forever ...
do    printf "New file (Return to finish): "
      read NEXTFILE
      if   [ -z "$NEXTFILE" ]
      then break # Leave the while loop
      fi
      cat $NEXTFILE >>$TMPFILE
done

cat $TMPFILE     # Print the temporary file
rm $TMPFILE      # Remove the temporary file
exit 0           # Exit cleanly

First of all, a filename is chosen to store the concatenated text as it is produced; a check is made to ensure that it does not in fact exist. This is necessary - in the unlikely event that another user had chosen the same temporary filename, and you did not make this check, the results of running the script would at best be unpredictable. A more sophisticated solution would try generating other filenames until it found one that did not exist. The script then loops continuously, requesting the user to enter a filename, reading that name from standard input, and storing it in the variable NEXTFILE. If NEXTFILE has zero length (i.e. the user has typed in a blank line) the loop is exited using break, otherwise the named file is appended to the end of the temporary file. Finally, after the loop has been exited, the temporary file is sent to standard output and then removed.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck