[Next] [Previous] [Up] [Top] [Contents] [Index]
Chapter 6: The UNIX File System
The UNIX system automatically puts you at a specific location in the file system when you log in. This is called your login directory. Typically, this is the same as your home directory. The name of your home directory is usually the same as your login name. Within this directory you can create files and additional directories (sometimes called subdirectories) in which to group the files. You can move and delete your own files and directories and control access to them.
The root of the file system is called root and is written as a slash (/). In other words, to change to the root directory, type:
% cd /
There is only one directory tree on a system even if several devices are mounted in that tree. (All devices are viewed as files.) The current directory or working directory is the directory that you are currently working in, which is also the directory that commands refer to by default. Files in your current directory can, therefore, be specified by their filenames only.
Wherever you can use a filename, you can also use a pathname, which is how you point to files that are not in your current directory. You can refer to files in other directories using either a relative path name, that is a path specified relative to your current directory, or with an absolute path name, that is a path specified relative to the root of the file system.
Absolute path names are preceded with /, the root directory. If a pathname does not begin with / it is assumed to be a relative path name. Relative path names begin with a directory or filename, a . (pronounced "dot") which refers to the current directory, or .. (pronounced "dot dot") which refers to the directory immediately above the current directory. The character / also separates components of the pathname, which are directory names, except for the last one, which can be either a simple filename or a directory name.
In summary, every file has a pathname, and its absolute pathname is of the form:
/rootdir/dir2/... /filename
The following is the form of a relative pathname of a file:
dir_n/dir_n+1/... /filename
An example of an absolute path name is:
/usr/smith/project1/afile
If my current directory is /usr/smith, then I can refer to the file afile in subdirectory project1 with a relative pathname like this:
project1/afile
Or, if my current directory is /usr/smith/project1, I can refer to a file named fileb in /usr/smith/project2 as:
../project2/fileb
[Missing image]Note that you cannot necessarily tell if fileb is an ordinary file or a directory name. Many commands will accept a directory name, and if it is a directory name, the command in which it is used may perform the action on all files in the directory. This behavior can be dangerous!
Your home directory is the top of your personal branch in the file system, and is usually designated by your username, i.e. /{path}/{username}.
In most UNIX shells other than sh, the tilde (~) stands for the home directory. Used alone, it specifies your home directory. Followed by a different user's login name, it expands into the pathname of the home directory of that user. This is a convenient way to refer to a user's directory, because it is independent of where the system manager may place the directory on the disk.
[Missing image]The use of tilde (~) to refer to a home directory is limited. It isn't available in the Bourne shell, and isn't available in FORTRAN.
Of the following three examples, the first refers to the file def from your own home directory, the second to the home directory of user jones, and the third refers to file data1 in the subdirectory project1 of jones' home directory.
~/def
~jones
~jones/project1/data1
To change to jones' home directory you'd enter:
% cd ~jones
FUE provides the command logdir which returns the full path of the specified user. logdir by itself returns the path of the invoking user. For example:
% logdir username
% echo ~username
To change directories you'd enter (note the use of backquotes to use the output of the enclosed string):
% cd `logdir username`
In contrast to the tilde, logdir can be used within commands, scripts, FORTRAN and C programs, and other programs in all shells.
The environment variable HOME is automatically set to the absolute pathname of your home directory. Environment variables are discussed in section 9.1 and HOME is described in section 9.2. To see the value of HOME, enter:
% echo $HOME
From some other directory, you can change to your home directory or one of its subdirectories using a command like the following:
% cd $HOME[23]
or
% cd $HOME/mysubdir
. Current directory
.. Parent directory of the current directory ("up" one directory)
~ Your home directory (all shells but sh)
Environment variable whose value is your home directory
~username Home directory of another user (all shells but sh)
/ Root directory
It is appropriate at this point to mention the relationship between directories and commands. A command is simply the name of an executable file, located in some directory. To execute a command, the shell first needs to find the executable file. The shell therefore needs to be given a set of directories to search. This information is provided via the environment variable PATH which is a list of search directories. You can display it with the command:
% echo $PATH
PATH is explained more thoroughly in section 9.2. Standard UNIX commands are generally grouped in a few standard directories (e.g., /usr/bin), and your default PATH contains these. See section 9.2 to learn how to run executables that you create and store in your own directories.
The utility which is useful in cases where a command may be ambiguous, for example due to aliasing (see section 9.3), and you want to know exactly which executable file or files the command runs. which lists the files that would be executed if the specified command(s) had been run. The syntax for which is:
% which command [command2 ...]
Each argument is expanded if it is aliased, and your path is searched for the executable files associated with the commands. See the man page for more information.