home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 1.20 Your Home Directory Chapter 1
Introduction
Next: 1.22 How UNIX Keeps Track of Files: Inodes
 

1.21 Making Pathnames

Pathnames locate a file (or directory, or any other object) in the UNIX filesystem. As you read this article, refer to Figure 1.4 . It's a diagram of a (very) small part of a UNIX filesystem.

Figure 1.4: Part of a UNIX Filesystem Tree

Figure 1.4

Whenever you are using UNIX, you have a current directory . By default, UNIX looks for any files or directories that you mention within the current directory. That is, if you don't give an absolute pathname (starting from the root, /  ), UNIX tries to look up files relative to the current directory. When you first log in, your current directory is your home directory ( 1.20 ) , which the system administrator will assign to you. It typically has a name like /usr/mike or /home/mike . You can change your current directory by giving the cd command, followed by the name of a new directory (for example, cd /usr/bin ). You can find out your current directory by giving the pwd ("print working directory") command.

If your current directory is /home/mike , and you give the command cat textfile , you are asking UNIX to locate the file textfile within the directory /home/mike . This is equivalent to the absolute path /home/mike/textfile . If you give the command cat notes/textfile , you are asking UNIX to locate the file textfile within the directory notes , within the current directory /home/mike .

A number of abbreviations help you to form relative pathnames more conveniently. You can use the abbreviation . (dot) to refer to the current working directory. You can use .. (dot dot) to refer to the parent of the current working directory. For example, if your current directory is /home/mike , ./textfile is the same as textfile , which is the same as /home/mike/textfile . The relative path ../gina/textfile is the same as /home/gina/textfile ; .. moves up one level from /home/mike (to /home ), and then searches for the directory gina and the file textfile .

In the C shell, ksh and bash , you can use the abbreviation ~ (tilde) to refer to your home directory. ~ name refers to the home directory of the user name . See article 14.11 .

Here's a summary of the rules that UNIX uses to interpret paths:

If the pathname begins with /

It is an absolute path, starting from the root.

If the pathname begins with ~ or with ~ name

The C shell, ksh and bash turn it into an absolute pathname starting at your home directory ( ~ ), or at the home directory of the user name ( ~ name ).

If the pathname does not begin with a /

The pathname is relative to the current directory. Two relative special cases use entries that are in every UNIX directory:

  1. If the pathname begins with ./  - the path is relative to the current directory; for example, ./textfile .

  2. If the pathname begins with ../  - the path is relative to the parent of the current directory. For example, if your current directory is /home/mike/work , then ../src means /home/mike/src .

Article 18.2 explains where . and .. come from.

NOTE: The . and .. may appear at any point within a path. They mean "the current directory at this point in the path" and "the parent of the current directory at this point in the path." You commonly see paths starting with ../../ (or more) to refer to the grandparent or great-grandparent of the current directory. However, they can appear at other places in a pathname as well. For example, /usr/ucb/./bin is the same as /usr/ucb/bin ; and /usr/ucb/bin/../lib is the same as /usr/ucb/lib . Placing . or .. in the middle of a path may be helpful in building paths within shell scripts, but I have never seen them used in any other useful way.

- ML , JP


Previous: 1.20 Your Home Directory UNIX Power Tools Next: 1.22 How UNIX Keeps Track of Files: Inodes
1.20 Your Home Directory Book Index 1.22 How UNIX Keeps Track of Files: Inodes

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System