The File System.


  1. General Layout of the File System.
    The logical file system is the hierarchy of the connected directories that contain all of the files that are accessible to the user. The UNIX file system is arranged in a tree where all of the files are contained in the root. Everything on the UNIX system is a file. There are three types of files.


    1. Data Files.
      UNIX does not know anything about the content of data files. The system keeps track of their location and size. For file that are fragmented, the system keeps track of separate disk allocations so that the file appears continuous to the user. Data files can contain data, text, binary data. Each program is responsible for managing its own data.

    2. Directory files.
      These are used by the system to create the hierarchical file system.

    3. Special files.
      Special files are actually devices. All device files are in a special directory /dev.

  2. File and Path Names.
    1. File names.
      Filenames can contain any symbol including unprintable characters. Stricly speaking UNIX does not use file suffixes. Some command like make or the web server use file suffixes but files are not required to have them by the OS.
      File names MUST be unique in their directory.

    2. Structure
      The file system is made up of a hierarchical structure such as:
                               / (root)
                               |
              +------+-------+-------+------------+
              |       |       |      |            |
             bin     etc     dev     lib          user
                                                  |
                                             +----+----+
                                             |         |
                                            sam       fred
                                                       |
                                                   +---+---+
                                                   |       |
                                                  data1   data2
      

    3. Working directory
      You are positioned in a directory. This is said to be your "working directory".

    4. . and ..
      In each directory there are two navigational files (. and ..) . is this directory. .. is the parent.

    5. Path
      The path to a file may be an absolute or relative path name.


      1. Absolute Path.
        The absolute path name starts at the root. /user/fred/data2 is the absolute path for the file data2 in the fred directory.

      2. Relative Path.
        Relative path name is from where you are at (working directory).

        1. If your working directory is /user/fred then the relative path name is just data2.

        2. If your working directory is /user the relative pathname is fred/data2.

        3. If your working directory is /user/sam then ../fred/data2 is the relative pathname.

  3. Directory Commands.

    1. pwd
      print the current working directory.

    2. cd
      change the working directory.

      1. If a directory is not specified then the value of $HOME is used.

      2. If a complete path is given then it becomes the new working directory ( a complete path starts with / . ..).

      3. If not a complete path name then cd tries to find the directory relative to the $CDPATH variable. If CDPATH is not defined the . is the default.

      4. To change to the home directory.
        $ cd
        $ pwd
        /home/kochis
        

      5. To change to a complete path.
        $ cd /etc
        $ pwd
        /etc
        

      6. To change relative to CDPATH
        $ pwd
        /etc
        $ echo $CDPATH
        /home/kochis
        $ cd Code
        /home/kochis/Code
        


    3. ls
      list the contents of a directory.
      $ pwd  
      /home/kochis/C
      
      $ ls -C
      AUP           Outline       Xstuff 
      CNP.Stats     Perl          app-w4w.frm 
      Code          Perl5         bin  
      

      The ls commands has many options: abcCdfFgilLmnopqrRstux. Some of the more useful options are -a list all.
      $ ls -a
       .                Getstats                      iconz.zip
       ..               GnLog                         ispell.4.0.tar
       .Xauthority      Hold                          ispell.word
       .Xde             Java                          junk
       .asWedit-prefs   Mail                          junk.html
       .ask             Misc                          keys
       .ask.events      Mystix                        lib-perl-5.09
       .bitmaps         Nets                          libwww.tar
      

      -l for a long list
      $ ls -al
      total 2096
      drwxr-xr-x   2 ask      staff        512 Jul 24 1996  .
      drwxr-xr-x  54 ask      staff       3584 Sep 24 07:28 ..
      -rw-r--r--   1 ask      staff       3783 Apr 15 1996  README
      -rw-r--r--   1 ask      staff       2476 Apr 15 1996  mitlicen.txt
      -rw-r--r--   1 ask      staff     827006 Apr 15 1996  pgp262s.tar.Z
      -rw-r--r--   1 ask      staff      83927 Apr 15 1996  pgpdoc1.txt
      -rw-r--r--   1 ask      staff     134070 Apr 15 1996  pgpdoc2.txt
      -rw-r--r--   1 ask      staff       7483 Apr 15 1996  rsalicen.txt
      

    4. mkdir
      make a directory
      $ mkdir new
      $ ls -Fd new
      new/
      

    5. rmdir
      delete a directory. A directory must be empty, have no files, in order to be removed.
      $ rmdir new
      $ rmdir C
      rmdir: C: File exists
      

  4. File Commands.

    1. cat
      concatenates files. The cat command reads files and writes them to standard output. To display or copy a file you can use cat like
      $ cat file
      

      It can be used to copy a file like:
      $ cat a.c >q.c
      

    2. pg or more
      allows you to look though a file a screen at a time.
      $ more a.c
      

    3. cp
      file copy command. The general format is:
      cp file(s) target
      If the target is a filename then cp copies a single file. If the target is a directory then cp can copy multiple files.
      $ cp setus newset
      $ mkdir newc
      $ cp c/*.c newc
      $ ls newc
      dir.c     signal.c  test.c
      

    4. mv
      move or rename files.
      The general format is:
      mv file(s) target
      To rename a file
      $ mv newset setter
      

      To move a file
      $ mv setter newc
      $ ls newc
      dir.c     setter    signal.c  test.c
      

      To rename a directory
      $ mv newc oldc
      


    5. rm
      removes files. The general format is:
      rm file(s)
      Two options used are -f to force removal and -i to confirm removal.
      $ rm setus
      $ rm -i oldc/*.c
      oldc/dir.c: ? y
      oldc/signal.c: ? y
      oldc/test.c: ? y
      

© Allan Kochis Last revision 8/31/2001