1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 99.2beta8 (1.46)
original version by: Nikos Drakos, CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>15. Symbolic and Hard Links</TITLE>
<META NAME="description" CONTENT="15. Symbolic and Hard Links">
<META NAME="keywords" CONTENT="rute">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v99.2beta8">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="rute.css">
<LINK REL="next" HREF="node19.html">
<LINK REL="previous" HREF="node17.html">
<LINK REL="up" HREF="rute.html">
<LINK REL="next" HREF="node19.html">
</HEAD>
<BODY BGCOLOR=#FFFFFF >
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<TR><TD align=left bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-purchase.html"><FONT COLOR=white>Purchase</FONT></A>
</FONT>
</TD><TD align=center bgcolor="#000000">
<FONT COLOR=white>
Copyright © 2002 Paul Sheer. <A HREF="copying.html"><FONT COLOR=white>Click here for copying permissions.</FONT></A>
</FONT>
</TD><TD align=right bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-home.html"><FONT COLOR=white>Home</FONT></A>
</FONT>
</TD></TR>
<TR><TD colspan=2 align=left bgcolor="#ECEBF4">
<IMG SRC="va-btn-small-light-60.png">
</TD><TD align=right bgcolor="#ECEBF4">
<IMG SRC="sflogo2-steel-60.png">
</TD></TR>
</TABLE><BR>
<!--Navigation Panel-->
<A NAME="tex2html2009"
HREF="node19.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2005"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html1999"
HREF="node17.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2007"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2010"
HREF="node19.html">16. Pre-installed Documentation</A>
<B> Up:</B> <A NAME="tex2html2006"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2000"
HREF="node17.html">14. Permission and Modification</A>
  <B> <A NAME="tex2html2008"
HREF="node1.html">Contents</A></B>
<BR>
<BR>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2011"
HREF="#SECTION001810000000000000000">15.1 Soft Links</A>
<LI><A NAME="tex2html2012"
HREF="#SECTION001820000000000000000">15.2 Hard Links</A>
</UL>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION001800000000000000000">
15. Symbolic and Hard Links</A>
</H1>
<P>
<A NAME="chap:links"></A>
<P>
Very often, a file is required to be in two different
directories at the same time. Think for example of a
configuration file that is required by two different software
packages that are looking for the file in different directories.
The file could simply be copied, but to have to replicate changes in more
than one place would create an administrative nightmare.
Also consider a document that must be present in
many directories, but which would be easier to update at one point.
<I>The way two (or more) files can have the same data is with links.</I>
<P>
<H1><A NAME="SECTION001810000000000000000">
15.1 Soft Links</A>
</H1>
<P>
To demonstrate a soft link, try the following:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>touch myfile</code><br>
<code>ln -s myfile myfile2</code><br>
<code>ls -al</code><br>
<code>cat > myfile</code><br>
<code>a</code><br>
<code>few</code><br>
<code>lines</code><br>
<code>of</code><br>
<code>text</code><br>
<code>^D</code><br>
<code>cat myfile</code><br>
<code>cat myfile2</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Notice that the <TT>
<FONT COLOR="#0000ff">ls -al</FONT></TT> listing has the letter
<TT>
<FONT COLOR="#0000ff">l</FONT></TT> on the far left next to <TT>
<FONT COLOR="#0000ff">myfile2</FONT></TT>, and the
usual <TT>
<FONT COLOR="#0000ff">-</FONT></TT> next to <TT>
<FONT COLOR="#0000ff">myfile</FONT></TT>. This indicates that
the file is a <I>soft</I> link (also known as a <I>symbolic</I>
link or <I>symlink</I>) to some other file.
<P>
A <I>symbolic link</I> contains no data of its own, only a
reference to another file. It can even contain a reference to
a directory. In either case, programs operating on the link
will actually see the file or directory it points to.
<P>
Try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>mkdir mydir</code><br>
<code>ln -s mydir mydir2</code><br>
<code>ls -al .</code><br>
<code>touch ./mydir/file1</code><br>
<code>touch ./mydir2/file2</code><br>
<code>ls -al ./mydir</code><br>
<code>ls -al ./mydir2</code><br>
</FONT></TD></TR></TABLE><P>
The directory <TT>
<FONT COLOR="#0000ff">mydir2</FONT></TT> is a symbolic link to <TT>
<FONT COLOR="#0000ff">mydir2</FONT></TT>
and appears as though it is a replica of the original. Once
again the directory <TT>
<FONT COLOR="#0000ff">mydir2</FONT></TT> does not consume additional
disk space--a program that reads from the link is unaware
that it is seeing into a different directory.
<P>
Symbolic links can also be copied and retain their value:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>cp mydir2 /</code><br>
<code>ls -al /</code><br>
<code>cd /mydir2</code><br>
</FONT></TD></TR></TABLE><P>
You have now copied the link to the root directory. However, the
link points to a relative path <TT>
<FONT COLOR="#0000ff">mydir</FONT></TT> in the same
directory as the link. Since there is no <TT>
<FONT COLOR="#0000ff">mydir</FONT></TT> here, an
error is raised.
<P>
Try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>rm -f mydir2 /mydir2</code><br>
<code>ln -s `pwd`/mydir mydir2</code><br>
<code>ls -al</code><br>
</FONT></TD></TR></TABLE><P>
Now you will see <TT>
<FONT COLOR="#0000ff">mydir2</FONT></TT> has an absolute path. You can try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>cp mydir2 /</code><br>
<code>ls -al /</code><br>
<code>cd /mydir2</code><br>
</FONT></TD></TR></TABLE><P>
and notice that it now works.
<P>
One of the common uses of symbolic links is to make
<I>mount</I>ed (see Section <A HREF="node22.html#sec:mount">19.4</A>) file systems
accessible from a different directory. For instance, you may
have a large directory that has to be split over several
physical disks. For clarity, you can mount the disks as
<TT>
<FONT COLOR="#0000ff">/disk1</FONT></TT>, <TT>
<FONT COLOR="#0000ff">/disk2</FONT></TT>, etc., and then link the various
subdirectories in a way that makes efficient use of the space
you have.
<P>
Another example is the linking of <TT>
<FONT COLOR="#0000ff">/dev/cdrom</FONT></TT> to, say,
<TT>
<FONT COLOR="#0000ff">/dev/hdc</FONT></TT> so that programs accessing the device
file <TT>
<FONT COLOR="#0000ff">/dev/cdrom</FONT></TT> (see Chapter <A HREF="node21.html#chap:unixdevices">18</A>)
actually access the correct IDE drive.
<P>
<H1><A NAME="SECTION001820000000000000000">
15.2 Hard Links</A>
</H1>
<P>
U<SMALL>NIX</SMALL> allows the data of a file to have more than one name in
separate places in the same file system. Such a file with more
than one name for the same data is called a <I>hard-linked</I>
file and is similar to a symbolic link. Try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>touch mydata</code><br>
<code>ln mydata mydataB</code><br>
<code>ls -al</code><br>
</FONT></TD></TR></TABLE><P>
The files <TT>
<FONT COLOR="#0000ff">mydata</FONT></TT> and <TT>
<FONT COLOR="#0000ff">mydataB</FONT></TT> are
indistinguishable. They share the same data, and have a
<TT>
<FONT COLOR="#0000ff">2</FONT></TT> in second column of the <TT>
<FONT COLOR="#0000ff">ls -al</FONT></TT> listing. This
means that they are hard-linked <I>twice</I> (that there are
two names for this file).
<P>
The reason why hard links are sometimes used in preference to
symbolic links is that some programs are not fooled by a
symbolic link: If you have, say, a script that uses <TT>
<FONT COLOR="#0000ff">cp</FONT></TT>
to copy a file, it will copy the symbolic link instead of the
file it points to. <FONT COLOR="#ffa500">[<TT>
<FONT COLOR="#0000ff">cp</FONT></TT> actually has an option to override
this behavior.]</FONT> A hard link, however, will always be seen as a
real file.
<P>
On the other hand, hard links cannot be made between files on different
file systems nor can they be made between directories.
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html2009"
HREF="node19.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html2005"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html1999"
HREF="node17.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html2007"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html2010"
HREF="node19.html">16. Pre-installed Documentation</A>
<B> Up:</B> <A NAME="tex2html2006"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html2000"
HREF="node17.html">14. Permission and Modification</A>
  <B> <A NAME="tex2html2008"
HREF="node1.html">Contents</A></B>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|