Skip Navigation
 Search | Directories | Reference Tools
UW Home > Discover UW > IT Connect > Unix 

Referencing Files From Programs and Scripts

Understanding proper file referencing is important Unix systems since automated processes move accounts around among available disks to make best use of available disk space. The purpose of this text is to show you how you can write scripts and programs such that they will not be "broken" by this automatic movement.

Hard-Coding File References Is Not A Good Approach

When you create a program that other people will run from their own accounts or that will be reading files located in other people's accounts, you need a way to reliably reference such files.

Hard coding references to files in other people's accounts is not a good approach. For example, it is not a good idea to use the following methods:

What you want is a method that will automatically update when accounts are moved.

Finding Users' Home Directories in the C Shell

If you are writing C shell script or working interactively in the C shell, you can use the tilde character, "~", which expands into the specification of your home directory or that of another user.

For example, to refer to your own home directory, you just put the tilde before the first slash, like this:

     ~/data/file-a

To refer to another users home directory, you include their login name after the tilde and before the first slash, like this:

     ~betty/data/file-z

Finding Users' Home Directories in the Bourne Shell

Many programmers write scripts in the Bourne Shell, which does not have the tilde character mechanism. It is possible, however, to use a C shell command from within a Bourne shell script:

      #!/bin/sh
      betty-home=`csh -fc "echo ~betty"`
      cat $betty-home/data/file-z

This same technique can be used in most other Unix shells, including the Bash and Korn shells.

Using the C Programming Language

The C programming language includes a library call that will return a structure that has in it the string specifying a given user's home directory. The string can then have appended to it whatever path you want before you use it to open() or stat() a file.

Here is an example C program that does the same thing as the simple script above:

      #include <sys/types.h>
      #include <unistd.h>
      #include <pwd.h>

      void main(void) {
         struct passwd *pwd;
         char command[1024];
         char betty_home[1024];

         pwd = getpwnam("betty");
         strcpy(betty_home, pwd->pw_dir);
         sprintf(command, "cat %s/data/file-z", betty_home);
         system(command);
         exit(0);
      }

Using the Perl Language

Perl, a popular interpreted language, has a function that also retrieves the current the location of a home directory.

Here is a Perl program equivalent to the C program above.

     #!/usr/local/bin/perl
     ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
       getpwnam("betty");
     $betty_home = $dir;
     $command = sprintf("cat %s/data/file-z", $betty_home);
     system($command);
     exit(0);

Further Information

The following is a partial list of books that cover the programming and shell use topics mentioned in this document. These books are available through the University of Washington Library system.

      Author:       Kochan, Stephen G.
      Title:        UNIX Shell programming
                    Stephen G. Kochan and Patrick H Wood.
      Pub. Info.:   Carmel, Ind., USA : Hayden Books, c1990.
      LC Subject:   UNIX-Computer-file.
                    UNIX-Shells.
      Author:       Kernighan, Brian W.
      Title:        The UNIX programming environment
                    Brian W. Kernighan, Rob Pike.
      Pub. Info.:   Englewood Cliffs, N.J. : Prentice-Hall, c1984.
      LC Subject:   UNIX-Computer-file.
                   Electronic-digital-computers -- Programming.

SEE ALSO

Man Pages:

movedir      - Move home directory and contents