\documentclass{ictlab}

\RCS $Revision: 1.1 $

\usepackage{alltt,key,color}
\ifx\pdftexversion\undefined \else
  \usepackage[pdfpagemode=None,pdfauthor={Nick Urbanik}]{hyperref}
\fi

\newcommand*{\labTitle}{Links: Hard Links and Symbolic Links}
\renewcommand*{\subject}{Operating Systems and Systems Integration}

\definecolor{light-blue}{rgb}{0.4,0.4,1}
\newcommand*{\gl}[1]{\textcolor{light-blue}{#1}} % good link
\newcommand*{\ex}[1]{\textcolor{green}{#1}} % executable file
\newcommand*{\bl}[1]{\colorbox{red}{\textcolor{white}{\textbf{#1}}}} % bad link

\begin{document}

\section{Aim}
\label{sec:aim}

You will understand hard and soft links to the point where you can use
them easily to solve many problems.

\section{Background}
\label{sec:background}

A \emph{link} gives another name to access a file by.

Links (particularly symbolic links) are incredibly useful in many
situations for the system administrator.  They can solve problems such
as installing software when the partition you must install it in
doesn't have enough space, although another partition does have enough
space.  There are very many other applications.


\subsection{Some Definitions}
\label{sec:definitions}

A \emph{directory} is a file that is has two entries for each file: a
\emph{file name} and an \emph{inode number}.  The inode number
uniquely identifies a data structure that contains all the information
about the file except its file name.  To see the directory entries as
they are stored in the directory, type:
\begin{alltt}
$ \textbf{ls -Ui \meta{directory-name}}
\end{alltt}%$

A \emph{hard link} is another directory entry for a file that has
another directory entry in the same partition.  Each of these links
has the same inode number.  If the links are in the same directory,
they must have different file names.

To create a hard link \emph{\texttt{link}} to the file
\emph{\texttt{source}}, type \texttt{ln \emph{source} \emph{link}}:
\begin{alltt}
$ \textbf{ln source link}
$ \textbf{ls -li}
total 8
 842707 -rw-r--r--    2 nicku    nicku        2681 Dec 16 20:46 link
 842707 -rw-r--r--    2 nicku    nicku        2681 Dec 16 20:46 source
\end{alltt}
Note that the source and the link both have the same inode number.
Both links are equally ``the file''.

A \emph{symbolic link} or \emph{soft link} is a small file that
contains the name of another file.  When you refer to the link, the
action takes place on the file whose name it contains.  We'll discuss
symbolic links in detail in section~\ref{sec:symbolic-links}.

\section{Limitations of Hard and Soft Links}
\label{sec:limitations}


\subsection{Hard Links}
\label{sec:hard-link-limitations}
\begin{itemize}
\item A hard link must be to a file that is not a directory and must
  be within the same disk partition.

\item The source file must exist.
  
\item It is hard to find all the links to the file if they are
  scattered in different places on the partition.
\end{itemize}

\subsection{Symbolic Links}
\label{sec:sym-link-limitations}

A symbolic link has few limitations, but there are some; the source
file must be visible from the position of the link.  Some applications
such as the ftp server have a different root directory from the main
directory system (using the \emph{chroot} system call).  Another case
is where a partition is mounted in a different place from usual.  In
such cases you need to use a \emph{relative} symbolic link.  A
relative symbolic link is linked to a name that does not start with a
\texttt{/}.  In general, it is often safer to use relative rather than
absolute symbolic links.

\section{All About Symbolic Links}
\label{sec:symbolic-links}

To create a symbolic link, you type:
\begin{alltt}
$ \textbf{ln -s \meta{source} \meta{link}}
\end{alltt}%$
The result will look something like this:
\begin{alltt}
$ \textbf{ln -s source link}
$ \textbf{ls -li source link}
 858645 lrwxrwxrwx    1 nicku    nicku           6 Dec 16 20:47 \gl{link} -> source
 858644 -rw-r--r--    1 nicku    nicku        2681 Dec 16 20:46 source
\end{alltt}
The size of this link is 6, the number of characters in the name
`source'.  The size of the link is equal to the number of characters
in the file that the link links to.  This is a \emph{relative symbolic
  link} because the name \texttt{source} does not begin with a \texttt{/}.

An important thing to understand is that the content of the symbolic
link (the name that it is linked to) is exactly what you type as the
first name in 
\begin{alltt}
$ \textbf{ln -s \meta{source} \meta{link}}
\end{alltt}%$

\subsection{Checking what file a link points to}

It is easy to check what file a link points to with the \texttt{-L}
option to \texttt{ls}:
\begin{alltt}
$ \textbf{ls -Lil source link}
 858644 -rw-r--r--    1 nicku    nicku        2681 Dec 16 20:46 link
 858644 -rw-r--r--    1 nicku    nicku        2681 Dec 16 20:46 source
\end{alltt}%$

\paragraph{Dangling links} are links that point to a file that does not
exist: 

\begin{alltt}
$ \textbf{ln -s foo bar}
$ \textbf{ls -l foo bar}
ls: foo: No such file or directory
lrwxrwxrwx    1 nicku    nicku           3 Dec 16 20:59 \bl{bar} -> \bl{foo}
\end{alltt}
The \texttt{ls} program colours bad links white on a red background
like \bl{this}.  Good links are coloured light-blue like \gl{this}.

\subsection{Making a link in another directory.}
\label{sec:another-dir}

%\newcommand*{\narrow}{\footnotesize}
\newcommand*{\narrow}{}

Some people get confused when making a link to a file in another
directory.  Here I am, in the \texttt{/bin} directory and I want to
make a link to the \texttt{ls} program in my $\sim$\texttt{/bin}
directory:
\begin{alltt}\narrow
1055 $ \textbf{pwd}
/bin
$ \textbf{ln -s ls \(\sim\)/bin}
$ \textbf{ls -l \(\sim\)/bin/ls}
lrwxrwxrwx   1 nicku  nicku   2 Dec 16 21:53 \bl{/home/nicku/bin/ls} -> \bl{ls}
\end{alltt}%$
%lrwxrwxrwx    1 nicku    nicku           2 Dec 16 21:53 \bl{/home/nicku/bin/ls} -> \bl{ls}
Oh dear, I got a dangling link!  How to get it right?  The thing to
realise is that whatever you type as the \emph{source} in\\[0.5ex]
\texttt{ln -s }\emph{source}\ \ \emph{link}\\[0.5ex]
is what will appear on the right side of arrow \texttt{->} when you
list the link with \texttt{ls -l}.  This is the right way to do it:
\begin{alltt}\narrow
$ \textbf{pwd}
/bin
$ \textbf{ln -s /bin/ls \(\sim\)/bin}
$ \textbf{ls -l \(\sim\)/bin/ls}
lrwxrwxrwx   1 nicku  nicku   7 Dec 16 22:05 \gl{/home/nicku/bin/ls} -> \ex{/bin/ls}
\end{alltt}%$
%lrwxrwxrwx    1 nicku    nicku           7 Dec 16 22:05 \gl{/home/nicku/bin/ls} -> \ex{/bin/ls}
Or we could make a \emph{relative link} (see
section~\ref{sec:sym-link-limitations}) like this:
\begin{alltt}\narrow
$ \textbf{pwd}
/bin
$ \textbf{ln -s ../../../bin/ls \(\sim\)/bin}
$ \textbf{ls -l \(\sim\)/bin/ls}
lrwxrwxrwx   1 nicku  nicku  15 Dec 16 22:11 \gl{/home/nicku/bin/ls} -> \ex{../../../bin/ls}
\end{alltt}%$
%lrwxrwxrwx    1 nicku    nicku          15 Dec 16 22:11 \gl{/home/nicku/bin/ls} -> \ex{../../../bin/ls}
The idea is that the \emph{source} name you type in your \texttt{ln
  -s} command is what you want to see on the right side of that arrow
\texttt{->} when you list the link with \texttt{ls -l}.
\end{document}
