Introduction to UNIX:
Lecture Two


 
2.1 Objectives

        This lecture covers:

  • The UNIX filesystem and directory structure.
  • File and directory handling commands.
  • How to make symbolic and hard links.
  • How wildcard filename expansion works.
  • What argument quoting is and when it should be used.
  • 2.2 The UNIX Filesystem
    The UNIX operating system is built around the concept of a filesystem which is used to store all of the information that constitutes the long-term state of the system. This state includes the operating system kernel itself, the executable files for the commands supported by the operating system, configuration information, temporary workfiles, user data, and various special files that are used to give controlled access to system hardware and operating system functions.

    Every item stored in a UNIX filesystem belongs to one of four types:

    1. Ordinary files

    2. Ordinary files can contain text, data, or program information. Files cannot contain other files or directories. Unlike other operating systems, UNIX filenames are not broken into a name part and an extension part (although extensions are still frequently used as a means to classify files). Instead they can contain any keyboard character except for '/' and be up to 256 characters long (note however that characters such as *,?,# and & have special meaning in most shells and should not therefore be used in filenames). Putting spaces in filenames also makes them difficult to manipulate - rather use the underscore '_'.
    3. Directories

    4. Directories are containers or folders that hold files, and other directories.
    5. Devices

    6. To provide applications with easy access to hardware devices, UNIX allows them to be used in much the same way as ordinary files. There are two types of devices in UNIX - block-oriented devices which transfer data in blocks (e.g. hard disks) and character-oriented devices that transfer data on a byte-by-byte basis (e.g. modems and dumb terminals).
    7. Links

    8. A link is a pointer to another file. There are two types of links - a hard link to a file is indistinguishable from the file itself. A soft link (or symbolic link) provides an indirect pointer or shortcut to a file. A soft link is implemented as a directory file entry containing a pathname.
    2.3 Typical UNIX Directory Structure
    The UNIX filesystem is laid out as a hierarchical tree structure which is anchored at a special top-level directory known as the root (designated by a slash '/'). Because of the tree structure, a directory can have many child directories, but only one parent directory. Fig. 2.1 illustrates this layout.


    Fig. 2.1: Part of a typical UNIX filesystem tree

    To specify a location in the directory hierarchy, we must specify a path through the tree. The path to a location can be defined by an absolute path from the root /, or as a relative path from the current working directory. To specify a path, each directory along the route from the source to the destination must be included in the path, with each directory in the sequence being separated by a slash. To help with the specification of relative paths, UNIX provides the shorthand "." for the current directory and ".." for the parent directory. For example, the absolute path to the directory "play" is /home/will/play, while the relative path to this directory from "zeb" is ../will/play.

    Fig. 2.2 shows some typical directories you will find on UNIX systems and briefly describes their contents. Note that these although these subdirectories appear as part of a seamless logical filesystem, they do not need be present on the same hard disk device; some may even be located on a remote machine and accessed across a network.
     

    Directory Typical Contents
    / The "root" directory
    /bin Essential low-level system utilities
    /usr/bin Higher-level system utilities and application programs
    /sbin Superuser system utilities (for performing system administration tasks)
    /lib Program libraries (collections of system calls that can be included in programs by a compiler) for low-level system utilities
    /usr/lib Program libraries for higher-level user programs
    /tmp Temporary file storage space (can be used by any user)
    /home or /homes User home directories containing personal file space for each user. Each directory is named after the login of the user.
    /etc UNIX system configuration and information files
    /dev Hardware devices
    /proc A pseudo-filesystem which is used as an interface to the kernel.  Includes a sub-directory for each active program (or process).
    Fig. 2.2: Typical UNIX directories

    When you log into UNIX, your current working directory is your user home directory. You can refer to your home directory at any time as "~" and the home directory of other users as "~<login>". So ~will/play is another way for user jane to specify an absolute path to the directory /homes/will/play. User will may refer to the directory as ~/play.
     

    2.4 Directory and File Handling Commands
    This section describes some of the more important directory and file handling commands.
    2.5 Making Hard and Soft (Symbolic) Links
    Direct (hard) and indirect (soft or symbolic) links from one file or directory to another can be created using the ln command.

        $ ln filename linkname

    creates another directory entry for filename called linkname (i.e. linkname is a hard link). Both directory entries appear identical (and both now have a link count of 2). If either filename or linkname is modified, the change will be reflected in the other file (since they are in fact just two different directory entries pointing to the same file).

        $ ln -s filename linkname

    creates a shortcut called linkname (i.e. linkname is a soft link). The shortcut appears as an entry with a special type ('l'):

        $ ln -s hello.txt bye.txt
        $ ls -l bye.txt
        lrwxrwxrwx   1 will finance 13 bye.txt -> hello.txt
        $

    The link count of the source file remains unaffected. Notice that the permission bits on a symbolic link are not used (always appearing as rwxrwxrwx). Instead the permissions on the link are determined by the permissions on the target (hello.txt in this case).

    Note that you can create a symbolic link to a file that doesn't exist, but not a hard link. Another difference between the two is that you can create symbolic links across different physical disk devices or partitions, but hard links are restricted to the same disk partition. Finally, most current UNIX implementations do not allow hard links to point to directories.

    2.6 Specifying multiple filenames
    Multiple filenames can be specified using special pattern-matching characters. The rules are: For example:
    1. ??? matches all three-character filenames.
    2. ?ell? matches any five-character filenames with 'ell' in the middle.
    3. he* matches any filename beginning with 'he'.
    4. [m-z]*[a-l] matches any filename that begins with a letter from 'm' to 'z' and ends in a letter from 'a' to 'l'.
    5. {/usr,}{/bin,/lib}/file expands to /usr/bin/file /usr/lib/file /bin/file and /lib/file.
    Note that the UNIX shell performs these expansions (including any filename matching) on a command's arguments before the command is executed.
    2.7 Quotes
    As we have seen certain special characters (e.g. '*', '-','{' etc.) are interpreted in a special way by the shell. In order to pass arguments that use these characters to commands directly (i.e. without filename expansion etc.), we need to use special quoting characters. There are three levels of quoting that you can try:
    1. Try insert a '\' in front of the special character.
    2. Use double quotes (") around arguments to prevent most expansions.
    3. Use single forward quotes (') around arguments to prevent all expansions.
    There is a fourth type of quoting in UNIX. Single backward quotes (`) are used to pass the output of some command as an input argument to another. For example:

        $ hostname
        rose
        $ echo this machine is called `hostname`
        this machine is called rose

    (BACK TO COURSE CONTENTS)



    © September 2001 William Knottenbelt (wjk@doc.ic.ac.uk)