old-www/LDP/LG/issue97/staff.html

127 lines
5.8 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><base href="http://www.linuxgazette.com/" />
<title>
Take Command: ln - Linux Gazette </title>
</head>
<body>
<!-- node: "Take Command: ln" -->
<table cellpadding="0" cellspacing="0" style="border 0px; width: 100%;">
<tr><td colspan="2"><img src="themes/lg/images/drop.gif" alt="" title="" /> &nbsp; <b>Take Command: ln</b></td></tr>
<tr style="vertical-align: bottom;"><td colspan="2" style="background-color: #000000; width: 100%;"><img src="themes/lg/images/pixel.gif" width="1" height="1" alt="" title="" /></td></tr>
<tr><td><div style="color: #7c7c7c;"><small>Submitted by <a href="http://www.linuxgazette.com/user/view/1116" title="View user profile.">staff</a> on Saturday, November 29, 2003 - 05:55</small></div></td><td style="text-align: right; vertical-align: top;"></td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2"><p>The ln command creates pseudonyms for files which allows them to be
accessed by different names. These pseudonyms are called links.
There are two different forms of the command and two different kinds of
links that can be created. First, let me explain the two forms.
<pre>
<b>ln</b> [<i>options</i>] <i>exiting_path</i> [<i>new_path</i>]
<b>ln</b> [<i>options</i>] <i>exiting_paths</i> <i>directory</i>
</pre>
<p>
In the first form, a new name is created called
<i>new_path</i> which is a psuedonym for
<i>existing_path</i>. The reason this is called a path is
that it can be a full pathname to a file. That is, it does not have to
specify a file in the current directory.
<p>
In the second form, the last argument is taken to be a directory name
and all the other arguments are paths to existing files. A link for
each existing file is created in the specified directory with the same
filename as the existing files.
<p>
Time for a few examples:
<br>
Create a link named my_file in the current directory to the file
/home/bill/his_file:<br>
<pre> ln /home/bill/his_file my_file </pre>
<p>
As above but the link is created in /home/joe/my_file:<br>
<pre> ln /home/bill/his_file /home/joe/my_file </pre>
<p>
As above but the link is named his_file and created in the current
directory:<br>
<pre> ln /home/bill/his_file </pre>
<p>
Here is an example of the second form where links to dog, cat and cow
from the current directory are created in /home/joe:<br>
<pre> ln dog cat cow /home/joe </pre>
<p>
Everything so far has created what is called a <i>hard link</i>. All
this means is that the new pseudonym has exactly the same properties as
the original name. In fact, the system makes on distinction between
them. For example, you could rename the file pig to chicken with the
following command sequence:
<pre>
ln pig chicken
rm pig
</pre>
<p>
The first line creates the pseudonym chicken for pig and the second
deletes pig. The filesystem is smart enough to know that as long as at
least one name points to the file, the file cannot be deleted.
<p>
All that said, there is a second kind of link called a symbolic link
which has quite different properties. That is, rather than pointing to
the file itself, it points to the file name (directory entry). This is
the only kind of link that can be used between filesystems. To create a
symbolic link, all works as above except you need to include the -s
option.
For example, to make a symbolic link called chicken that points to pig,
you would say:<br>
<pre> ln -s pig chicken</pre>
<p>
The only way you will see that chicken is a symbolic link is by using
the ls -l command (<code>ls -l chicken</code>). The output of this
command will look much like this:<br>
<pre> lrwxrwxrwx 1 joe users 3 2003-11-18 17:26 chicken -> pig</pre>
<br>
The first character (l) indicates that this is a link and the <code>chicken
-> pig</code> part indicates that chicken is a pointer to pig.
<p>
Now, if you were to delete the file pig (<code>rm pig</code>) the link
named chicken would still exist but any attempt to reference it (for
example, cat chicken) would give you a file not found error message.
<p>
Like most Linux commands, there are lots more options and lots more
choices. If you enter <code>ln --help</code> you will see the
complete list of options.
<p>
The first line creates the pseudonym chicken for pig and the second
deletes pig. The filesystem is smart enough to know that as long as at
least one name points to the file, the file cannot be deleted.
<p>
All that said, there is a second kind of link called a symbolic link
which has quite different properties. That is, rather than pointing to
the file itself, it points to the file name (directory entry). This is
the only kind of link that can be used between filesystems. To create a
symbolic link, all works as above except you need to include the -s
option.
For example, to make a symbolic link called chicken that points to pig,
you would say:<br>
<pre> ln -s pig chicken</pre>
<p>
The only way you will see that chicken is a symbolic link is by using
the ls -l command (<code>ls -l chicken</code>). The output of this
command will look much like this:<br>
<pre> lrwxrwxrwx 1 joe users 3 2003-11-18 17:26 chicken -> pig</pre>
<br>
The first character (l) indicates that this is a link and the <code>chicken
-> pig</code> part indicates that chicken is a pointer to pig.
<p>
Now, if you were to delete the file pig (<code>rm pig</code>) the link
named chicken would still exist but any attempt to reference it (for
example, cat chicken) would give you a file not found error message.
<p>
Like most Linux commands, there are lots more options and lots more
choices. If you enter <code>ln --help</code> you will see the
complete list of options.</p></td></tr>
</table>
</body>
</html>