% Linux Installation and Getting Started    -*- TeX -*-
% links.tex
% Copyright (c) 1992, 1993 by Matt Welsh <mdw@sunsite.unc.edu>
%
% This file is freely redistributable, but you must preserve this copyright 
% notice on all copies, and it must be distributed only as part of "Linux 
% Installation and Getting Started". This file's use is covered by the 
% copyright for the entire document, in the file "copyright.tex".

\section{Managing file links}\label{sec-manage-links}
\index{files!links|(}
\index{links|(}
\index{inode number!defined}
\index{files!inode numbers of}
Links allow you to give a single file multiple names. Files are
actually identified to the system by their {\bf inode number}, which is
just the unique filesystem identifier for the file\footnote{The command
{\tt ls -i} will display file inode numbers.}. A directory is actually
a listing of inode numbers with their corresponding filenames. Each 
filename in a directory is a {\bf link} to a particular inode. 

\subsection{Hard links}
\index{links!hard}
The {\tt ln} command is used to create multiple links for one
file. For example, let's say that you have the file {\tt foo} in 
a directory. Using {\tt ls -i}, we can look at the inode number
for this file.
\begin{tscreen}
\# {\em ls -i foo} \\
22192 foo \\
\#
\end{tscreen}
Here, the file {\tt foo} has an inode number of 22192 in the
filesystem. We can create another link to {\tt foo}, named {\tt bar}:
\begin{tscreen}
\# {\em ln foo bar} 
\end{tscreen}
With {\tt ls -i}, we see that the two files have the same inode.
\begin{tscreen}
\# {\em ls -i foo bar} \\
22192 bar\ \ \ 22192 foo \\
\#
\end{tscreen}
Now, accessing either {\tt foo} or {\tt bar} will access
the same file. If you make changes to {\tt foo}, those changes
will be made to {\tt bar} as well. For all purposes, {\tt foo}
and {\tt bar} are the same file.

These links are known as {\em hard links\/} because they directly
create a link to an inode. Note that you can only hard-link files on
the same filesystem; symbolic links (see below) don't have this
restriction.

When you delete a file with {\tt rm}, you are actually only 
deleting one link to a file. If you use the command
\begin{tscreen}
\# {\em rm foo}
\end{tscreen}
then only the link named {\tt foo} is deleted; {\tt bar}
will still exist. A file is only actually deleted on the system
when it has no links to it. Usually, files have only one
link, so using the {\tt rm} command deletes the file. However,
if a file has multiple links to it, using {\tt rm} will only
delete a single link; in order to delete the file, you must
delete all links to the file.

\index{links!display number of}
The command {\tt ls -l} will display the number of links to a
file (among other information).
\begin{tscreen}
\# {\em ls -l foo bar} \\
\verb!-rw-r--r--   2 root     root          12 Aug  5 16:51 bar! \\
\verb!-rw-r--r--   2 root     root          12 Aug  5 16:50 foo! \\
\#
\end{tscreen}
The second column in the listing, ``{\tt 2}'', specifies the number of
links to the file. 

As it turns out, a directory is actually just a file containing information
about link-to-inode translations. Also, every directory has at least two
hard links in it: ``{\tt .}'' (a link pointing to itself), and 
``{\tt ..}'' (a link
pointing to the parent directory). The root directory ({\tt /}) 
``{\tt ..}'' link
just points back to {\tt /}. 

\subsection{Symbolic links}
\index{links!symbolic}
Symbolic links are another type of link, which are somewhat different
than hard links.
A symbolic link allows you to give a file another name, but it
doesn't link the file by inode.

The command {\tt ln -s} will create a symbolic link to a file.
For example, if we use the command
\begin{tscreen}
\# {\em ln -s foo bar}
\end{tscreen} 
we will create the symbolic link {\tt bar} pointing to the file
{\tt foo}. If we use {\tt ls -i}, we will see that the two
files have different inodes, indeed.
\begin{tscreen}
\# {\em ls -i foo bar} \\
22195 bar\ \ \ 22192 foo \\
\#
\end{tscreen}
However, using {\tt ls -l}, we see that the file {\tt bar}
is a symlink pointing to {\tt foo}.
\begin{tscreen}
\# {\em ls -l foo bar} \\
\verb!lrwxrwxrwx   1 root     root           3 Aug  5 16:51 bar -> foo! \\
\verb!-rw-r--r--   1 root     root          12 Aug  5 16:50 foo! \\
\#
\end{tscreen}

The permission bits on a symbolic link are not used (they always
appear as {\tt rwxrwxrwx}). Instead, the permissions on the symbolic link 
are determined by the permissions on the target of the symbolic link (in
our example, the file {\tt foo}).

Functionally, hard links and symbolic links are similar, but
there are some differences. For one thing,
you can create a symbolic link to a file which doesn't exist; the
same is not true for hard links. Symbolic links are processed by 
the kernel differently than hard links are, which is just a
technical difference but sometimes an important one. Symbolic links
are helpful because they identify what file they point to; with
hard links, there is no easy way to determine which files are linked
to the same inode.

Links are used in many places on the Linux system. Symbolic links
are especially important to the shared library images in 
{\tt /lib}. See Section~\ref{sec-upgrade-libs} for more information.
\index{files!links|)}
\index{links|)}
