All data stored in an Unix system is accessed by a file name and the list of all file names are stored in the file system directory tree. Therefore, it is important to understand how to move around the Unix directory tree.
To access a file listed on the directory tree, the user must specify its name and location. In some cases, the command shell provides support and the specification action is transparent but it still occurs.
There are two ways to specify the location of a file to be accessed :
An absolute path describes the location of the file of interest starting from the root directory file identified by the forward slash (root) at the beginning of the path description.
cat /usr/include/stdio.h
Keep in mind only the 1st slash represents a real directory name, all other slashes act as delimiters between directory and directory or directory and file.
The above statement indicates that we wish to display (cat) the contents of the file "stdio.h" listed in the "include" directory which in turn is listed in the "user" directory which is listed in the root "/" directory. Since the OS can always find root, as long as all other path information is correct, the file of interest will be found.
When ever you use $HOME or ~ to reference your home directory, you access the absolute path to your home directory.
The alternative method of locating a file is to access the file via a relative path. The relative path describes the location of the file of interest relative to the current working directory.
When a user logs in, the user's home directory is usually set as the default "current working directory". If the user chooses to change to a different directory, that becomes the current working directory. The user can always display the current working directory with the command "pwd"
An example of using a relative path :
gcc assn.c
This indicates that the user wishes to use the Gnu c compiler on the source file pgm1.c located in the current working directory. Notice there is no / in front of the filename.
The primary advantage of relative paths is that it saves time in typing.
The Unix system provides a couple of special filenames to help work with relative paths.
The 1st special filename is . (dot) and indicates the current working directory. . (dot) is usually not needed when accessing a file in the current working directory as data.
However, if the file represents a command, it may be needed. Unix uses a list of directory paths stored in the variable PATH to locate the command requested. It looks only in those directories and will it search the directories in the order they are stored in PATH.
To bypass the PATH search, specify the location of the program to run with the the relative path from the current directory (dot).
./mypgm
This indicates that the program of interest can be found by looking in the current directory.
The 2nd special filename is .. (dot dot) and indicates the parent directory of the current directory. All directories will have a .. listed as thier parent directory including root.
To access the parent directory, simply specify ..
ls ..
lists the contents of the parent of the current directory.
Exercise : Log into your account. #( To display the absolute path of the current directory, run the command : ) pwd #( Also try the following : )
echo $PWD #( Depending on command interpreter, one or more of these should display the full path of your home directory. ) #( To display the contents of the parent directory relative to the current directory, run the command : ) ls .. #( Make sure you are in your home directory. Then using the parent directory as a relative starting point, list the contents of your home directory. ) cd $HOME #( For the next command, substitue your actual z-id for z#. This command will translate to list the file z# found listed in the parent directory of the current directory. ) ls ../z# #( This is a listing of my home directory relative to yours. ) ls ../berezin
|