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 |
~ |
which is your home directory. So |
~username |
which is the home directory of the user username. |
- |
which is the directory where we came from. So |
Exercises
We said that cd and cd ~ are equivalent. In which situation should we use ~ ?
In the table is said that - stands for “the directory where we came from.” Is this always correct ?
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
What happens if you go up “off the tree” ?
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 ?