📄 ch18_06.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.6 Stale Symbolic Links </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:39:01Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch18_01.htm"TITLE="18. Linking, Renaming, and Copying Files"><LINKREL="prev"HREF="ch18_05.htm"TITLE="18.5 Creating and Removing Links "><LINKREL="next"HREF="ch18_07.htm"TITLE="18.7 Linking Directories "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch18_05.htm"TITLE="18.5 Creating and Removing Links "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.5 Creating and Removing Links "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 18<BR>Linking, Renaming, and Copying Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch18_07.htm"TITLE="18.7 Linking Directories "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.7 Linking Directories "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5751">18.6 Stale Symbolic Links </A></H2><PCLASS="para">Symbolic links have one problem. Like good bread, they become "stale"fairly easily. What does that mean?</P><PCLASS="para">Consider the following commands:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ln -s foo bar</B></CODE>% <CODECLASS="userinput"><B>rm foo</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">What happens when you do this?Remember that the link <EMCLASS="emphasis">bar</EM> is a pointer: it doesn't have anyreal data of its own. Its data is the name of the file <EMCLASS="emphasis">foo</EM>.After deleting <EMCLASS="emphasis">foo</EM>, the link <EMCLASS="emphasis">bar</EM> still exists, but itpoints to a nonexistent file. Commands that refer to <EMCLASS="emphasis">bar</EM> willget a confusing error message:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat bar</B></CODE>cat: bar: No such file or directory</PRE></BLOCKQUOTE></P><PCLASS="para">This will drive you crazy if you're not careful. An <EMCLASS="emphasis">ls</EM> willshow you that <EMCLASS="emphasis">bar</EM> still exists. You won't understand what'sgoing on until you realize that <EMCLASS="emphasis">bar</EM> is only a pointer to a filethat no longer exists.<ACLASS="indexterm"NAME="AUTOID-20481"></A><ACLASS="indexterm"NAME="AUTOID-20484"></A>[The command <EMCLASS="emphasis">ls -Ll</EM> or <EMCLASS="emphasis">ls -LF</EM> will show an unconnectedsymbolic link.The <EMCLASS="emphasis">-L</EM> option means "list the file that this link points toinstead of the link itself."If the link points nowhere, <EMCLASS="emphasis">ls -L</EM> will still list the link. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20493"></A><ACLASS="indexterm"NAME="AUTOID-20496"></A><ACLASS="indexterm"NAME="AUTOID-20498"></A>There are many innocuous ways of creating invalid symbolic links. Forexample, you could simply <EMCLASS="emphasis">mv</EM> the data file <EMCLASS="emphasis">foo</EM>. Or youcould move <EMCLASS="emphasis">foo</EM>, <EMCLASS="emphasis">bar</EM>, or both to some other part of thefilesystem where the pointer wouldn't be valid anymore.</P><PCLASS="para">One way to avoid problems with invalid links is to use<SPANCLASS="link">relative pathnames (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>when it is appropriate.For example, using relativepathnames will let you move entire directory trees around withoutinvalidating links (providing that both the file and the link are inthe same tree). Here's an example. Assume that you have the file <EMCLASS="emphasis">/home/mars/john/project/datastash/input123.txt</EM>. Assume thatyou want to link this file to <EMCLASS="emphasis">/home/mars/john/test/input.txt</EM>.You create a link by giving the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd /home/mars/john/test</B></CODE>% <CODECLASS="userinput"><B>ln -s ../project/datastash/input123.txt input.txt</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">At some later date, you hand the project over to <EMCLASS="emphasis">mary</EM>, who<SPANCLASS="link">copies (<ACLASS="linkend"HREF="ch18_16.htm"TITLE="Copying Directory Trees with (tar | tar) ">18.16</A>)</SPAN>the entire <EMCLASS="emphasis">project</EM> and <EMCLASS="emphasis">test</EM> data trees intoher home directory. The link between <EMCLASS="emphasis">input.txt</EM> and the realfile, <EMCLASS="emphasis">input123.txt</EM>, will still be valid. Although both files'names have changed, the relationship between the two (i.e., therelative path from one directory to the other) is still the same.Alternatively, assume that you are assigned to a different computernamed <EMCLASS="emphasis">jupiter</EM> and that you copy your entire home directorywhen you move. Again, the link remains valid: the relative path fromyour <EMCLASS="emphasis">test</EM> directory to your <EMCLASS="emphasis">datastash</EM> directory hasn'tchanged, even though the absolute paths of both directories aredifferent.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20524"></A>On the other hand, there is certainly room for<SPANCLASS="link">absolute pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>.They're useful if you're more likely to move the link than theoriginal file.Assume that you are creating a link from your working directory to afile in a master directory (let's say<EMCLASS="emphasis">/corp/masterdata/input345.txt</EM>). It is much more likely that you will rearrange your working directory than that someone will move themaster set of files. In this case, you would link as follows:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ln -s /corp/masterdata/input345.txt input.txt</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Now you can move the link <EMCLASS="emphasis">input.txt</EM> anywhere in the filesystem:it will still be valid, provided that <EMCLASS="emphasis">input345.txt</EM> never moves.</P><PCLASS="para">In article<ACLASS="xref"HREF="ch16_28.htm"TITLE="oldlinks: Find Unconnected Symbolic Links ">16.28</A>,we give a script for detecting stale symbolic links.</P><PCLASS="para">Note that<SPANCLASS="link">hard links (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>never have this problem. With a hard link,there is no difference at all between the link and theoriginal - in fact, it's unfair to call one file the link and theother the original.<ACLASS="indexterm"NAME="AUTOID-20538"></A><ACLASS="indexterm"NAME="AUTOID-20539"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch18_05.htm"TITLE="18.5 Creating and Removing Links "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.5 Creating and Removing Links "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch18_07.htm"TITLE="18.7 Linking Directories "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.7 Linking Directories "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">18.5 Creating and Removing Links </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">18.7 Linking Directories </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -