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

📄 ch45_18.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.18 Using basename and dirname </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:54:42Z"><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="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><LINKREL="prev"HREF="ch45_17.htm"TITLE="45.17 Making a for Loop with Multiple Variables "><LINKREL="next"HREF="ch45_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands "></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="ch45_17.htm"TITLE="45.17 Making a for Loop with Multiple Variables "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.17 Making a for Loop with Multiple Variables "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 45<BR>Shell Programming for the Initiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.19 A while Loop with Several Loop Control Commands "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2260">45.18 Using basename and dirname </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="basename">basename</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="dirname">dirname</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-53389"></A><ACLASS="indexterm"NAME="AUTOID-53391"></A><ACLASS="indexterm"NAME="AUTOID-53393"></A><ACLASS="indexterm"NAME="AUTOID-53396"></A>Almost every UNIX command can use<SPANCLASS="link">relative and absolute pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>tofind a file or directory.There are times you'll need part of a pathname&nbsp;- the head (everythingbefore the last slash) or the tail (the name after the last slash).The utilities <EMCLASS="emphasis">basename</EM> and <EMCLASS="emphasis">dirname</EM>, available on mostUNIX systems (and on the CD-ROM) handle that.</TD></TR></TABLE><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2260-SECT-1.1">45.18.1 Introduction to basename and dirname </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53407"></A>The <EMCLASS="emphasis">basename</EM> command strips any &quot;path&quot; name components from afilename, leaving you with a &quot;pure&quot; filename.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>basename /usr/bin/gigiplot</B></CODE>gigiplot% <CODECLASS="userinput"><B>basename /home/mikel/bin/bvurns.sh</B></CODE>bvurns.sh</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">basename</EM> can also strip a suffix from a filename.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>basename /home/mikel/bin/bvurns.sh .sh</B></CODE>bvurns</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53421"></A><ACLASS="indexterm"NAME="AUTOID-53424"></A>The <EMCLASS="emphasis">dirname</EM> command strips the filename itself, giving you the&quot;directory&quot; part of the pathname:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>dirname /usr/bin/screenblank</B></CODE>/usr/bin% <CODECLASS="userinput"><B>dirname local</B></CODE>.</PRE></BLOCKQUOTE></P><PCLASS="para">If you give <EMCLASS="emphasis">dirname</EM> a &quot;pure&quot; filename (i.e., a filename with nopath, as in the second example), it tells you that the directory is<CODECLASS="literal">.</CODE> (the current directory).</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> <ACLASS="indexterm"NAME="AUTOID-53437"></A><ACLASS="indexterm"NAME="AUTOID-53440"></A><EMCLASS="emphasis">dirname</EM> and <EMCLASS="emphasis">basename</EM> have a bug in many System V implementations.They don't recognize the second argument as a filename suffix to strip.Here's a good test:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>basename 0.foo .foo</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If the answer is <CODECLASS="literal">0</CODE>, your <EMCLASS="emphasis">basename</EM> implementation is good.If the answer is <CODECLASS="literal">0.foo</CODE>, the implementation is bad.If <EMCLASS="emphasis">basename</EM> doesn't work, <EMCLASS="emphasis">dirname</EM> won't, either.</P></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2260-SECT-1.2">45.18.2 Use with Loops </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53457"></A><ACLASS="indexterm"NAME="AUTOID-53460"></A><ACLASS="indexterm"NAME="AUTOID-53463"></A><ACLASS="indexterm"NAME="AUTOID-53466"></A><ACLASS="indexterm"NAME="AUTOID-53469"></A><ACLASS="indexterm"NAME="AUTOID-53472"></A><ACLASS="indexterm"NAME="AUTOID-53475"></A>Here's an example of <EMCLASS="emphasis">basename</EM> and <EMCLASS="emphasis">dirname</EM>.There's a directory tree with some very large files&nbsp;- over 100,000characters.You want to find those files, run<SPANCLASS="link"><EMCLASS="emphasis">split</EM> (<ACLASS="linkend"HREF="ch35_09.htm"TITLE="Splitting Files at Fixed Points: split ">35.9</A>)</SPAN>on them, and add <EMCLASS="emphasis">huge.</EM> to the start of the original filename.By default, <EMCLASS="emphasis">split</EM> names the file chunks <EMCLASS="emphasis">xaa</EM>, <EMCLASS="emphasis">xab</EM>,<EMCLASS="emphasis">xac</EM>, andso on; you want to use the original filename and a dot (<CODECLASS="literal">.</CODE>)instead of <EMCLASS="emphasis">x</EM>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch44_09.htm"TITLE="44.9 Testing Your Success ">||</A> <ACLASS="co"HREF="ch44_11.htm"TITLE="44.11 Set Exit Status of a Shell (Script) ">exit</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">for path in `find /home/you -type f -size +100000c -print`do    cd `dirname $path` || exit    filename=`basename $path`    split $filename $filename.    mv -i $filename huge.$filenamedone</PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53493"></A>The <EMCLASS="emphasis">find</EM> command will output pathnames like these:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/home/you/somefile/home/you/subdir/anotherfile</PRE></BLOCKQUOTE></P><PCLASS="para">(The absolute pathnames are important here.The <EMCLASS="emphasis">cd</EM> would fail on the second pass of the loop if youuse relative pathnames.)In the loop, the <EMCLASS="emphasis">cd</EM> command uses <EMCLASS="emphasis">dirname</EM> to go to thedirectory where the file is.The <EMCLASS="emphasis">filename</EM> variable, with the output of <EMCLASS="emphasis">basename</EM>, is usedseveral places&nbsp;- twice on the <EMCLASS="emphasis">split</EM> command line.</P><PCLASS="para">If that gives the error <CODECLASS="literal">command line too long</CODE>, replace the firstlines with the two lines below.This makes a<SPANCLASS="link">redirected-input loop (<ACLASS="linkend"HREF="ch45_22.htm"TITLE="Handling Files Line-by-Line ">45.22</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">find /home/you -type f -size +100000c -print |while read path</PRE></BLOCKQUOTE></P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN>, <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="ch45_17.htm"TITLE="45.17 Making a for Loop with Multiple Variables "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.17 Making a for Loop with Multiple Variables "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="ch45_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.19 A while Loop with Several Loop Control Commands "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.17 Making a for Loop with Multiple Variables </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">45.19 A while Loop with Several Loop Control Commands </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 + -