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

📄 ch18_15.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.15 Copying Directory Trees with cp -r </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:39:12Z"><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_14.htm"TITLE="18.14 Relinking Multiple Symbolic Links "><LINKREL="next"HREF="ch18_16.htm"TITLE="18.16 Copying Directory Trees with (tar | tar) "></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_14.htm"TITLE="18.14 Relinking Multiple Symbolic Links "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.14 Relinking Multiple Symbolic 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_16.htm"TITLE="18.16 Copying Directory Trees with (tar | tar) "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.16 Copying Directory Trees with (tar | tar) "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5480">18.15 Copying Directory Trees with cp -r </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-5480-IX-CP-COMMAND-R-OPTION"></A><ACLASS="indexterm"NAME="UPT-ART-5480-IX-TREES-DIRECTORY-COPYING"></A><ACLASS="indexterm"NAME="UPT-ART-5480-IX-COPYING-DIRECTORY-TREES"></A><ACLASS="indexterm"NAME="AUTOID-20951"></A>Some versions of <EMCLASS="emphasis">cp</EM> have a <EMCLASS="emphasis">-r</EM> (recursive) flag.It copies all the files in a directorytree&nbsp;- that is, all the files in a directory and its subdirectories.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> One of our UNIX systems has a <EMCLASS="emphasis">cp</EM> without a <EMCLASS="emphasis">-r</EM> option.But it also has an<ACLASS="indexterm"NAME="AUTOID-20960"></A><SPANCLASS="link"><EMCLASS="emphasis">rcp</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>command that <EMCLASS="emphasis">does</EM> have <EMCLASS="emphasis">-r</EM>.<EMCLASS="emphasis">rcp</EM> can copy to any machine, not just remote machines.When I need <EMCLASS="emphasis">cp -r</EM> on that host, I use <EMCLASS="emphasis">rcp -r</EM>.</P></BLOCKQUOTE><PCLASS="para">The first argument(s) to <EMCLASS="emphasis">cp -r</EM> can be directory(s)-or, if you name anyfile(s), they'll be copied just the way they would without the <EMCLASS="emphasis">-r</EM>.The last argument should be a directory.So, you can use <EMCLASS="emphasis">cp -r</EM> in two ways:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Give <EMCLASS="emphasis">cp -r</EM> directory(s) to copy.They'll be created as subdirectories of the directory named at the end.</P></LI><LICLASS="listitem"><PCLASS="para">Give <EMCLASS="emphasis">cp -r</EM> file(s) to copy.They'll be copied to the directory named at the end.</P></LI></UL><PCLASS="para">Those two methods are really doing the same thing.They're both copying the tail of the first pathname(s) to the end of the lastpathname.</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Here's how to do the copy shown in<ACLASS="xref"HREF="ch18_15.htm#UPT-ART-5480-FIG-0"TITLE="Copying /home/jane to Current Directory (/work/bkup) with cp">Figure 18.1</A>.This copies the directory <EMCLASS="emphasis">/home/jane</EM>, with all its files andsubdirectories, and creates a subdirectory named <EMCLASS="emphasis">jane</EM> in the<SPANCLASS="link">current directory <CODECLASS="literal">.</CODE> (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd /work/bkup</B></CODE>% <CODECLASS="userinput"><B>cp -r /home/jane .</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">How can you copy the contents of the subdirectory called <EMCLASS="emphasis">data</EM> and allits files (but not the subdirectory itself) into aduplicate directory named <EMCLASS="emphasis">data.bak</EM>?First create the destinationdirectory.That's because the last argument to <EMCLASS="emphasis">cp -r</EM> must be a directory thatalready exists:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd /home/jane</B></CODE>% <CODECLASS="userinput"><B>mkdir data.bak</B></CODE>% <CODECLASS="userinput"><B>cp -r data/* data.bak</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That doesn't copy any files in <EMCLASS="emphasis">data</EM> whose names start with a dot(<CODECLASS="literal">.</CODE>).There's<SPANCLASS="link">a way (<ACLASS="linkend"HREF="ch15_05.htm"TITLE='Matching All "Dot Files&quot; with Wildcards'>15.5</A>)</SPAN>to do that though.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-5480-FIG-0">Figure 18.1: Copying /home/jane to Current Directory (/work/bkup) with cp</A></H4><IMGCLASS="graphic"SRC="figs/5480.gif"ALT="Figure 18.1"></LI><LICLASS="listitem"><PCLASS="para">To copy the subdirectories <EMCLASS="emphasis">Sep</EM> and<EMCLASS="emphasis">Oct</EM> and their files, as well as the file <EMCLASS="emphasis">Output</EM>, fromthe directory <EMCLASS="emphasis">/home/jim/calendar</EM> into the current directory (<CODECLASS="literal">.</CODE>):</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch15_02.htm"TITLE="15.2 Filename Wildcards in a Nutshell ">[..]*</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>cp -r /home/jim/calendar/[SO]* .</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">If you use the C shell or <EMCLASS="emphasis">bash</EM>, you can copy just the directories by using thehandy<SPANCLASS="link">curly brace operators (<ACLASS="linkend"HREF="ch09_05.htm"TITLE="Build Strings with {&nbsp;} ">9.5</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cp -r /home/jim/calendar/{Sep,Oct} .</B></CODE></PRE></BLOCKQUOTE></P></LI></UL><PCLASS="para">Some gotchas:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Symbolic and hard links (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN><ACLASS="indexterm"NAME="AUTOID-21034"></A><ACLASS="indexterm"NAME="AUTOID-21037"></A><ACLASS="indexterm"NAME="AUTOID-21040"></A><ACLASS="indexterm"NAME="AUTOID-21043"></A>are copied as files.That can be good because, at the destination, a symbolic link mightpoint to the wrong place.It can be bad if the link pointed to a really big file;the copy can take a lot of disk space.(In<ACLASS="xref"HREF="ch18_15.htm#UPT-ART-5480-FIG-0"TITLE="Copying /home/jane to Current Directory (/work/bkup) with cp">Figure 18.1</A>notice that the symbolic link in <EMCLASS="emphasis">jane</EM>'s homedirectory was converted to a file named <EMCLASS="emphasis">.setup</EM> with a copy of thecontents of <EMCLASS="emphasis">generic</EM>.)&#13;</P></LI><LICLASS="listitem"><PCLASS="para">On many UNIXes, the copy will be dated at the time you made the copy andmay have its permissions set by your<SPANCLASS="link"><EMCLASS="emphasis">umask</EM> (<ACLASS="linkend"HREF="ch22_04.htm"TITLE="Setting an Exact umask ">22.4</A>)</SPAN>.If you want the copy to have the originalmodification time and permissions,add the <EMCLASS="emphasis">-p</EM> option.</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">cp -r</EM> will go into an endless loop if you name a directory inthe list to copy from and also as the destination directory.For example, let's say you're copying everything from the current directoryinto an existing subdirectory named <EMCLASS="emphasis">backup</EM>, like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cp -r * backup</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Unless your <EMCLASS="emphasis">cp -r</EM> works differently from the ones I've tried, itwill create <EMCLASS="emphasis">backup/backup</EM>, and <EMCLASS="emphasis">backup/backup/backup</EM>, and so on.To avoid that, replace the <CODECLASS="literal">*</CODE> wildcard with other less-&quot;wild&quot;wildcards.You can also match everything except the destination directory name byusing<SPANCLASS="link">the <EMCLASS="emphasis">ksh</EM> <CODECLASS="literal">!</CODE> operator, the <EMCLASS="emphasis">tcsh</EM> <CODECLASS="literal">^</CODE> operator (<ACLASS="linkend"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>)</SPAN>,or<SPANCLASS="link">the <EMCLASS="emphasis">nom</EM> script (<ACLASS="linkend"HREF="ch15_09.htm"TITLE="nom: List Files that Don't Match a Wildcard ">15.9</A>)</SPAN>.</P></LI></UL><ACLASS="indexterm"NAME="AUTOID-21074"></A><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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_14.htm"TITLE="18.14 Relinking Multiple Symbolic Links "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.14 Relinking Multiple Symbolic 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_16.htm"TITLE="18.16 Copying Directory Trees with (tar | tar) "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.16 Copying Directory Trees with (tar | tar) "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">18.14 Relinking Multiple Symbolic 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.16 Copying Directory Trees with (tar | tar) </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 + -