PRO-DEVELOP

2007/12/26

Unix : Filenames,Relative Pathnames

Filenames
In UNIX, every file and directory has a name associated with it. This name is referred to as the file or
directory's filename.
In addition to their filenames, every file and directory is associated with the name of its parent directory.
When a filename is combined with the parent directory's name, the result is called a pathname. Two
examples of pathnames are
/home/ranga/docs/book/ch5.doc
/usr/local/bin/
As you can see, each of these pathnames consists of several "words" separated by the slash ( /) character.
In UNIX, the slash separates directories, whereas the individual words are the names of files or directories.
The sum of all the words and the / characters makes up the pathname.
The last set of characters in a pathname is the actual name of the file or directory being referred to: The rest
of the characters represent its parent directories. In the first example, the filename is ch5.doc.
The name of a file can be up to 255 characters long and can contain any ASCII character except /.
Generally, the characters used in pathnames are the alphanumeric characters ( a to z, A to Z, and 0 to 9)
along with periods ( .), hyphens ( -), and underscores ( _).
Other characters, especially the space, are usually avoided because many programs cannot deal with them
properly. For example, consider a file with the following name:
A Farewell To Arms
Most programs treat this a four separate files named A, Farewell, To, and Arms, instead of one file.
One thing to keep in mind about filenames is that two files in the same directory cannot have the same
name. Thus both of the following filenames
/home/ranga/docs/ch5.doc
/home/ranga/docs/ch5.doc
refer to the same file, but the following filenames
/home/ranga/docs/ch5.doc
/home/ranga/docs/books/ch5.doc
refer to different files because they are located in different directories. In addition, because UNIX is casesensitive,
you can have two files in the same directory whose names differ only by case. UNIX considers the
following
/home/ranga/docs/ch5.doc
/home/ranga/docs/CH5.doc
to be different files. This often confuses users coming from the Windows or DOS environments.

Relative Pathnames
A relative pathname enables you to access files and directories by specifying a path to that file or
directory within your current directory. When your current directory changes, the relative pathname to a file
can also change.
To find out what the current directory is, use the pwd (print working directory ) command, which
prints the name of the directory in which you are currently located. For example
$ pwd
/home/ranga/pub
tells me that I am located in the directory /home/ranga/pub.
When you're specifying a relative pathname, the slash character is not present at the beginning of the
pathname. This indicates that a relative pathname is being used instead of an absolute pathname. The
relative pathname is a list of the directories located between your current directory and the file or directory
you are representing.
If you are pointing to a directory in your pathname that is below your current one, you can access it by
specifying its name. For example, the directory name:
docs/
refers to the directory docs located in the current directory.
In order to access the current directory's parent directory or other directories at a higher level in the tree
than the current level, use the special name of two dots ( ..).
The UNIX file system uses two dots ( ..) to represent the directory above you in the tree, and a single dot (
.) to represent your current directory.
Look at an example that illustrates how relative pathnames are used. Assume that the current directory is
/home/ranga/work
Then the relative pathname

../docs/ch5.doc
represents the file
/home/ranga/docs/ch5.doc
whereas
./docs/ch5.doc
represents the file
/home/ranga/work/docs/ch5.doc
You can also refer to this file using the following relative path:
docs/ch5.doc
As mentioned previously, you do not have to append the ./ to the beginning of pathnames that refer to files
or directories located within the current directory or one of its subdirectories.