📄 ch18_11.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.11 Renaming Files with ren </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:08Z"><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_10.htm"TITLE="18.10 There's More than One Way to Do It "><LINKREL="next"HREF="ch18_12.htm"TITLE="18.12 Renaming a List of Files Interactively "></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_10.htm"TITLE="18.10 There's More than One Way to Do It "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.10 There's More than One Way to Do It "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_12.htm"TITLE="18.12 Renaming a List of Files Interactively "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.12 Renaming a List of Files Interactively "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7918">18.11 Renaming Files with ren </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20754"></A>The CD-ROM contains a commandcalled <EMCLASS="emphasis">ren</EM> that you can use to rename multiple files.The advantage of <EMCLASS="emphasis">ren</EM> is that it can be used to rename files in aflexible fashion. For example, I have a set of PostScript filesthat are named <EMCLASS="emphasis">ps.ch01</EM>, <EMCLASS="emphasis">ps.ch02</EM>, and so on. I need thesefiles to follow the usual convention of having the <EMCLASS="emphasis">ps</EM> extension as a suffix, not a prefix - i.e.<EMCLASS="emphasis">ch01.ps</EM>, <EMCLASS="emphasis">ch02.ps</EM>, <EMCLASS="emphasis">ch03.ps</EM>, etc. I could do thiswith a simple shell script, but it's much easier to just use <EMCLASS="emphasis">ren</EM>.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="ren">ren</A><BR></TH><TDVALIGN="TOP"> <EMCLASS="emphasis">ren</EM> recognizes the metacharacters <CODECLASS="literal">*</CODE> and <CODECLASS="literal">?</CODE>,and uses each instance of their use in replacement patterns. Thefirst string in the filename that matches a wildcard is taken asargument 1, or <CODECLASS="literal">#1</CODE>. The second is taken as <CODECLASS="literal">#2</CODE>, andso on. The best way to explain this is to simply show how <EMCLASS="emphasis">ren</EM>can be used. </TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>ps.ch01 ps.ch02 ps.ch03 ps.ch04 ps.ch05 ps.ch06 ps.ch07</PRE></BLOCKQUOTE></P><PCLASS="para">Use the <CODECLASS="literal">*</CODE> wildcard in the search string, and then use <CODECLASS="literal">#1</CODE>where you want that string included in the replacement string. Because <CODECLASS="literal">*</CODE> is also a shell wildcard that should be interpretedby <EMCLASS="emphasis">ren</EM> and not the shell, youneed to protect it within<SPANCLASS="link">quotes (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>.In the Bourne shell, <CODECLASS="literal">#</CODE> is also a comment character and needs tobe quoted; an interactive C shell doesn't treat <CODECLASS="literal">#</CODE> as a comment but,to beconsistent, we showit quoted: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ren "ps.*" "#1.ps"</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If <EMCLASS="emphasis">ren</EM> completes execution silently, everything worked just fine and the files were renamed. Check by listing the directoryagain:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>ch01.ps ch02.ps ch03.ps ch04.ps ch05.ps ch06.ps ch07.ps</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">ren</EM> doesn't let you overwrite existing files without warning. Suppose we had anotherfile in the same directory called <EMCLASS="emphasis">ch07.ps</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>ch07.ps ps.ch01 ps.ch02 ps.ch03 ps.ch04 ps.ch05 ps.ch06 ps.ch07</PRE></BLOCKQUOTE></P><PCLASS="para">Now when we try renaming the files, <EMCLASS="emphasis">ren</EM> warns you about overwriting the <EMCLASS="emphasis">ch07.ps</EM> file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ren "ps.*" "#1.ps"</B></CODE>ps.ch07 -> ch07.ps ; remove old ch07.ps?</PRE></BLOCKQUOTE></P><PCLASS="para">This feature can be suppressed with the <EMCLASS="emphasis">-d</EM> option, which says tooverwrite files without prompting. Related options are <EMCLASS="emphasis">-k</EM>, which says <EMCLASS="emphasis">not</EM> to overwrite anyfiles, also without prompting; and <EMCLASS="emphasis">-a</EM>, which says to abortthe entire procedure if any files will be overwritten. Using<EMCLASS="emphasis">-a</EM>, <EMCLASS="emphasis">ren</EM> aborts before any files are renamed, so youcan start all over again.</P><PCLASS="para"><EMCLASS="emphasis">ren</EM> is also smart enough to detect internal naming conflicts before itactually renames any files. For example, suppose we had both fileswith both <EMCLASS="emphasis">ps.</EM> and <EMCLASS="emphasis">eps.</EM> prefixes that we wanted renamedwith <EMCLASS="emphasis">.ps</EM> suffixes. If therewere any conflicts, <EMCLASS="emphasis">ren</EM> wouldtell us right away, and none of the files would be renamed:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>README ps.ch01 ps.ch03 ps.ch05 ps.ch07eps.ch07 ps.ch02 ps.ch04 ps.ch06% <CODECLASS="userinput"><B>ren "*ps.*" "#2.ps"</B></CODE>Two or more files would have to be renamed to 'ch07.ps'.Aborting, no renames done.</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">ren</EM> has the restriction that it can only be used to move fileswithin a single directory. Although this makes it inconvenient forsome applications, it also makes it more secure. </P><PCLASS="para">To show <EMCLASS="emphasis">ren</EM> in a more complicated situation, let's take another example. Every week I write a report andthen store it in a directory under the name <EMCLASS="emphasis">month</EM>.<EMCLASS="emphasis">day</EM>.<EMCLASS="emphasis">year</EM>.After a while, I realized that because of the default sorting used by<EMCLASS="emphasis">ls</EM>, the files weren't being listed in chronological order.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>1.13.92 1.27.92 12.23.91 2.3.921.20.92 1.6.92 12.30.91</PRE></BLOCKQUOTE></P><PCLASS="para">What I needed to do was to rename them <EMCLASS="emphasis">year</EM>.<EMCLASS="emphasis">month</EM>.<EMCLASS="emphasis">day</EM>,and use leading 0s for the first nine months. This can be quicklydone with two <EMCLASS="emphasis">ren</EM> commands: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ren "?.*.*" "#3.0#1.#2"</B></CODE>% <CODECLASS="userinput"><B>ren "1?.*.9?" "9#3.1#1.#2"</B></CODE>% <CODECLASS="userinput"><B>ls</B></CODE>91.12.23 92.01.13 92.01.27 92.02.391.12.30 92.01.20 92.01.6</PRE></BLOCKQUOTE></P><PCLASS="para">The first command renames any reports for single-digit months (0-9).In the second command, I'm careful not to match any of the namesof files I've already moved.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">LM</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_10.htm"TITLE="18.10 There's More than One Way to Do It "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.10 There's More than One Way to Do It "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_12.htm"TITLE="18.12 Renaming a List of Files Interactively "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.12 Renaming a List of Files Interactively "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">18.10 There's More than One Way to Do It </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.12 Renaming a List of Files Interactively </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 + -