4.1.4. Full pathnames and relative pathnames

When we did cd, we always used full pathnames. This means a name of a directory or file starting with a ‘/’. This is also called absolute pathname because we give exactly the path starting from the root (‘/’) to the destination.

We can also use relative pathnames, pathnames which are related to the directory where we are standing. These pathnames don’t start with a ‘/’. The shell interpretes this pathname as starting from the present directory. For example,

% cd /usr/local
% cd etc

leads to /usr/local/etc. The second cd-command means “go to the directory with name ‘etc’ which is a subdirectory of the present directory (/usr/local)” The command cd /etc, in contrast, goes always to the ‘etc’ directory under the root (/etc).

From /usr/local/etc we can go back to /usr/local by cd .., which means, go to the directory where this directory is a subdirectory of (go to the parent directory). This notation can be stacked. cd ../.. goes two levels up in the directory tree. (In fact two levels towards the root, which isn’t up for a normal tree.) These rules also apply to other commands such as ls, or any command that expects a directory or file name as argument.

Other special pathnames are:

.

which is the current directory. So cd . doesn’t do anything.

~

which is your home directory. So cd ~ or cd do the same thing.

~username

which is the home directory of the user username.

-

which is the directory where we came from. So cd - goes back to the previous directory This gives an error if there is no previous directory (e.g just after login)




Exercises


  1. We said that cd and cd ~ are equivalent. In which situation should we use ~ ?

  2. In the table is said that - stands for “the directory where we came from.” Is this always correct ?

  3. Can you find out in which directory you are, after running these commands ? (Every command starts in your home-directory.) First try to find out without trying.

    • cd ..

    • cd ../sysmn

    • cd ~sysmn (what is the difference with the preceding ? Compare these commands for the user ‘puppet’)

    • cd /usr/local/games/../share

    • Run the previous command with ‘games’ replaced by ‘chem’ Explain what you see.

    • cd /usr/local/games ; cd ../share (the ; separates 2 commands on 1 line)

    • cd /var/log ; cd -

    • cd var/log ; cd -

    • cd ../../usr/bin

  4. What happens if you go up “off the tree” ?

  5. The command realpath can show the path found (resolved path).

    What is the result of the next commands (first consider what should be the result):

    • realpath /usr/local/games/../share

    • realpath /usr/local/chem/../share

    What is happening ? How can this be solved ?