📄 ch18_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.4 More About 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:38:57Z"><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_03.htm"TITLE="18.3 Files with Two or More Names"><LINKREL="next"HREF="ch18_05.htm"TITLE="18.5 Creating and Removing Links "></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_03.htm"TITLE="18.3 Files with Two or More Names"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.3 Files with Two or More Names"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_05.htm"TITLE="18.5 Creating and Removing Links "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.5 Creating and Removing Links "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7944">18.4 More About Links </A></H2><PCLASS="para">UNIX provides two different kinds of links:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-7944-IX-LINKS"></A><ACLASS="indexterm"NAME="AUTOID-20234"></A><ACLASS="indexterm"NAME="AUTOID-20236"></A><ACLASS="indexterm"NAME="AUTOID-20239"></A><ACLASS="indexterm"NAME="AUTOID-20242"></A><BCLASS="emphasis.bold">Hard links</B>: With a hard link, two filenames (i.e., two<SPANCLASS="link">directory entries (<ACLASS="linkend"HREF="ch18_02.htm"TITLE="What's Really in a Directory ">18.2</A>)</SPAN>)point to the sameinodeand thesame set of data blocks.All UNIX versions support hard links.They have two importantlimitations: a hard link can't cross a filesystem (i.e., bothfilenames must be in the same filesystem), and you can't create a hardlink to a directory (i.e., a directory can only have one name).[1]They have two important advantages: the link and the original fileare absolutely and always identical, and the extra link takes no diskspace (except an occasional extra disk block in the directory file).</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] Actually, every directory has at least two names.See the last section of this article.</P></BLOCKQUOTE></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20252"></A><BCLASS="emphasis.bold">Symbolic links</B> (also called <BCLASS="emphasis.bold">soft links</B> or <BCLASS="emphasis.bold">symlinks</B>):With a symboliclink, there really are twodifferent files.One file contains the actual data; the other filejust contains the name of the first file and serves as a "pointer."We call the pointer the <EMCLASS="emphasis">link</EM>.The system knowsthat whenever it opens a link, it should read the contents of thelink, and then access the file that really holds the data you want.All Berkeley UNIX systems and System V.4 support symbolic links.Symbolic links are infinitely more flexible than hard links.They cancross filesystems, or even computer systems (if you are using NFS orRFS). You can make a symbolic link to a directory.A symbolic link has its own inode and takes a small amount of disk spaceto store.</P></LI></UL><PCLASS="para">You obviously can't do without copies of files: copies are importantwhenever users need their own "private version" of some master file.But it is also important to know about links.With links, there'sonly one set of data and many different names that can access it.Article<ACLASS="xref"HREF="ch18_05.htm"TITLE="Creating and Removing Links ">18.5</A>shows how to make links.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7944-SECT-1.1">18.4.1 Differences Between Hard and Symbolic Links </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20263"></A><ACLASS="indexterm"NAME="AUTOID-20266"></A>With a hard link, the two filenames are identical in everyway.You can delete one without harming the other.The systemdeletes the directory entry for one filename and leaves the data blocks(which are shared) untouched.The only thing <EMCLASS="emphasis">rm</EM> does to theinode is decrement its "link count," which (as the name implies) countsthe number of hard links to the file.The data blocks are only deletedwhen the link count goes to zero - meaning that there are no moredirectory entries that point to this inode.Article<ACLASS="xref"HREF="ch17_22.htm"TITLE="Finding the Links to a File ">17.22</A>shows how to find the hard links to a file.</P><PCLASS="para">With a symbolic link, the two filenames are really not the same.Deleting the link with <EMCLASS="emphasis">rm</EM>leaves the original file untouched, which is whatyou'd expect.But deleting or renaming the original file removes boththe filename and the data.You are left with a link that doesn'tpoint anywhere.(Article<ACLASS="xref"HREF="ch16_28.htm"TITLE="oldlinks: Find Unconnected Symbolic Links ">16.28</A>has a script that finds unconnected symlinks.)Remember that the link itself doesn't have any dataassociated with it.Despite this disadvantage, you rarely see hardlinks on UNIX versions that support symbolic links.Symbolic linksare so much more versatile that they have become omnipresent.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20275"></A>Let's finish by taking a look at the <EMCLASS="emphasis">ls</EM> listing for a directory.This directory has a file named <EMCLASS="emphasis">file</EM> with another hard link to itnamed <EMCLASS="emphasis">hardlink</EM>.There's also a symlink to <EMCLASS="emphasis">file</EM> named (are you ready?) <EMCLASS="emphasis">symlink</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>ls -lai</B></CODE>total 8 140330 drwxr-xr-x 2 jerry ora 1024 Aug 18 10:11 . 85523 drwxr-xr-x 4 jerry ora 1024 Aug 18 10:47 .. 140331 -rw-r--r-- 2 jerry ora 2764 Aug 18 10:11 file 140331 -rw-r--r-- 2 jerry ora 2764 Aug 18 10:11 hardlink 140332 lrwxrwxrwx 1 jerry ora 4 Aug 18 10:11 symlink -> file</PRE></BLOCKQUOTE></P><PCLASS="para">You've seen the<SPANCLASS="link"><EMCLASS="emphasis">-l</EM> option (<ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>and also, probably, the<SPANCLASS="link"><EMCLASS="emphasis">-a</EM> option (<ACLASS="linkend"HREF="ch16_11.htm"TITLE="Showing Hidden Files with ls -A and -a ">16.11</A>)</SPAN>for listing "dot files."<ACLASS="indexterm"NAME="AUTOID-20291"></A>The <EMCLASS="emphasis">-i</EM> option lists the<SPANCLASS="link">i-number (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>for each<SPANCLASS="link">entry in the directory (<ACLASS="linkend"HREF="ch18_02.htm"TITLE="What's Really in a Directory ">18.2</A>)</SPAN>;see the first column.The third column has the <EMCLASS="emphasis">link count</EM>: this is the number of hard linksto the file.</P><PCLASS="para">When you compare the entries for <EMCLASS="emphasis">file</EM> and <EMCLASS="emphasis">hardlink</EM>, you'll seethat they have a link count of 2.In this case, both links are in the same directory.Every other entry (i-number, size, owner, etc.) for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -