⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch18_03.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.3 Files with Two or More Names</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:38:54Z"><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_02.htm"TITLE="18.2 What's Really in a Directory "><LINKREL="next"HREF="ch18_04.htm"TITLE="18.4 More About 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_02.htm"TITLE="18.2 What's Really in a Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.2 What's Really in a Directory "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_04.htm"TITLE="18.4 More About Links "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.4 More About Links "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1470">18.3 Files with Two or More Names</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1470-IX-FILES-NAMING"></A><ACLASS="indexterm"NAME="UPT-ART-1470-IX-NAMING-MULTIPLE-NAMES-FOR-ONE-FILE"></A><ACLASS="indexterm"NAME="UPT-ART-1470-IX-MULTIPLE-NAMES-FOR-ONE-FILE"></A><ACLASS="indexterm"NAME="UPT-ART-1470-IX-SYMBOLIC-LINKS-ADVANTAGES"></A>We talk about hard links and symbolic links in<SPANCLASS="link">a number of places (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>, <ACLASS="linkend"HREF="ch18_05.htm"TITLE="Creating and Removing Links ">18.5</A>, <ACLASS="linkend"HREF="ch24_04.htm"TITLE="Save Space with a Link ">24.4</A>)</SPAN>.However, we've never really said <EMCLASS="emphasis">why</EM> you'd want a file withseveral names. When I was learning UNIX, this was a big stumblingblock. It was easy to understand what a link would <EMCLASS="emphasis">do</EM>, but whywould you want one?</P><PCLASS="para">With time, I acquired wisdom. There are many situations that links(and only links) are able to handle. Once you've seen a few of theproblems that a link can solve, you'll start seeing even moresituations in which they are appropriate.</P><PCLASS="para">Consider a companyphone list on a system that is shared by several users. Every usermight want a copy of the phone list in his or her home directory.However, you wouldn't want to give each user a different phone list.In addition to wasting disk space, it would be a pain to modify all ofthe individual lists whenever you made a change. Giving each user a&quot;link&quot; to a master phone list is one way to solve the problem.</P><PCLASS="para">Similarly, assumethat you use several different systems that share files via<SPANCLASS="link">NFS (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>.Eventually, you get tired of editing five or six different <SPANCLASS="link"><EMCLASS="emphasis">.login</EM> and <EMCLASS="emphasis">.cshrc</EM> (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>files whenever you decide to add a newalias or change some element in your startup file; you'd like to havethe exact same file appear in eachof your home directories. You might also want to give severalsystems access to the same master database files.</P><PCLASS="para">How about this: you have a program or script that performs severalrelated functions.Why not perform them all with the sameexecutable?All the script or program needs to do is check thename it's called by, and act accordingly.Article<ACLASS="xref"HREF="ch45_13.htm"TITLE="Save Disk Space and Programming: Multiple Names for a Program">45.13</A>explains how this works;articles<ACLASS="xref"HREF="ch08_08.htm"TITLE="A Directory for Commands You Shouldn't Run ">8.8</A>,<ACLASS="xref"HREF="ch16_07.htm"TITLE="ls Shortcuts: ll, lf, lg, etc. ">16.7</A>,and <ACLASS="xref"HREF="ch22_10.htm"TITLE="cx, cw, c-w: Quick File Permission Changes ">22.10</A>show scripts that act differently depending on their (current) name.</P><PCLASS="para">Yet another example.Assume that you have two versions of a file: a current version,which changes from time to time, and one or more older versions. Onegood convention would be to name the files <CODECLASS="literal">data.</CODE><CODECLASS="replaceable"><I>date</I></CODE>, where<CODECLASS="replaceable"><I>date</I></CODE> shows when the file was created. Forexample, you might have the files <EMCLASS="emphasis">data.jul1</EM>, <EMCLASS="emphasis">data.jul2</EM>,<EMCLASS="emphasis">data.jul5</EM>, and so on. However, when you access these files, youdon't necessarily want to figure out the date&nbsp;- not unless youhave a better chronological sense than I do. To make it easier onyourself, create a link (either symbolic or hard) named <EMCLASS="emphasis">data.cur</EM>that always refers to your most recent file.The following script runs the program <EMCLASS="emphasis">output</EM>, puts thedata into a dated file, and resets <EMCLASS="emphasis">data.cur</EM>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch51_10.htm"TITLE="51.10 The date Command ">+%h%d</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">#!/bin/shcurfile=data.`date +%h%d`linkname=data.curoutput &gt; $curfilerm -f $linknameln -s $curfile $linkname</PRE></TD></TR></TABLE></P><PCLASS="para">Here's an analogous problem. When writing technical manuals at onecompany, I had two classes of readers: some insisted on referring tothe manuals by name, and the others by (believe it or not) part number.Rather than looking up part numbers all the time, I created a set oflinks so that I could look up a manual online either via its name orvia its part number. For example, if the manual was named&quot;Programming&quot; and had the part number 046-56-3343, I would createthe file <EMCLASS="emphasis">/manuals/byname/programming</EM>. I would then create the link<EMCLASS="emphasis">/manuals/bynumber/046-56-3343</EM>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch01_21.htm"TITLE="1.21 Making Pathnames ">..</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd /manuals/bynumber</B></CODE>% <CODECLASS="userinput"><B>ln -s ../byname/programming 046-56-3343</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">Sometimes you simply want to collect an assortment of files in onedirectory. These files may really belong in other places, but youwant to collect them for some temporary purpose: for example, to makea tape.Let's say that you want to make a tape thatincludes manual pages from <EMCLASS="emphasis">/development/doc/man/man1</EM>, a manualfrom <EMCLASS="emphasis">/development/doc/product</EM>, source files from<EMCLASS="emphasis">/src/ccode</EM>, and a set of executables from<EMCLASS="emphasis">/release/68000/execs</EM>.The shell script below creates links for all of thesedirectories within the <EMCLASS="emphasis">/tmp/tape</EM> directory, and then creates a<SPANCLASS="link"><EMCLASS="emphasis">tar</EM> (<ACLASS="linkend"HREF="ch20_01.htm#UPT-ART-8024"TITLE="tar in a Nutshell ">20.1</A>)</SPAN>tape that can be sent to a customer or friend.Note that the <EMCLASS="emphasis">tar&nbsp;h</EM> option tells <EMCLASS="emphasis">tar</EM> to follow symbolic links andarchive whatever is at the end of the link; otherwise, <EMCLASS="emphasis">tar</EM>makes a copy of just the symbolic link:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch21_02.htm"TITLE="21.2 A Better Place for Temporary Files: /tmp ">/tmp</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">#!/bin/shmkdir /tmp/tapecd /tmp/taperm -f man1 product ccode execsln -s /development/doc/man/man1ln -s /development/doc/productln -s /src/ccodeln -s /release/68000/execstar ch ./man1 ./product ./ccode ./execs</PRE></TD></TR></TABLE></P><PCLASS="para">This really only scrapes the surface.Links provide neat solutions tomany problems, including source control, filesystem layout, etc.<ACLASS="indexterm"NAME="AUTOID-20219"></A><ACLASS="indexterm"NAME="AUTOID-20220"></A><ACLASS="indexterm"NAME="AUTOID-20221"></A><ACLASS="indexterm"NAME="AUTOID-20222"></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_02.htm"TITLE="18.2 What's Really in a Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.2 What's Really in a Directory "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_04.htm"TITLE="18.4 More About Links "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.4 More About Links "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">18.2 What's Really in a Directory </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.4 More About Links </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 &amp; 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 + -