📄 ch18_16.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.16 Copying Directory Trees with (tar | tar) </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:14Z"><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_15.htm"TITLE="18.15 Copying Directory Trees with cp -r "><LINKREL="next"HREF="ch19_01.htm"TITLE="19. Creating and Reading Archives"></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_15.htm"TITLE="18.15 Copying Directory Trees with cp -r "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.15 Copying Directory Trees with cp -r "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="CHAPTER"HREF="ch19_01.htm"TITLE="19. Creating and Reading Archives"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 19. Creating and Reading Archives"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6890">18.16 Copying Directory Trees with (tar | tar) </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-21080"></A>The<SPANCLASS="link"><EMCLASS="emphasis">tar</EM> (<ACLASS="linkend"HREF="ch19_05.htm"TITLE="Using tar to Create and Unpack Archives ">19.5</A>)</SPAN>command isn't just for tape archives.It can copy files from disk to disk, too.And even if your computer has<SPANCLASS="link"><EMCLASS="emphasis">cp -r</EM> (<ACLASS="linkend"HREF="ch18_15.htm"TITLE="Copying Directory Trees with cp -r ">18.15</A>)</SPAN>,there are advantages to using <EMCLASS="emphasis">tar</EM>.</P><PCLASS="para">The obvious way to copy directories with <EMCLASS="emphasis">tar</EM> is to write them ontoa tape archive with relative pathnames - then read back the tape andwrite it somewhere else on the disk.But <EMCLASS="emphasis">tar</EM> can also write to a UNIX pipe - and read from a pipe.This looks like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="replaceable"><I>reading-tar</I></CODE> <CODECLASS="userinput"><B>|</B></CODE> <CODECLASS="replaceable"><I>writing-tar</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">with one trick:the <CODECLASS="replaceable"><I>writing-tar</I></CODE> process<SPANCLASS="link">has a different current directory (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>, <ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>(the place where you want the copy made) than the <CODECLASS="replaceable"><I>reading-tar</I></CODE>.To do that, run the <CODECLASS="replaceable"><I>writing-tar</I></CODE> in a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>.</P><PCLASS="para">The argument(s) to the <CODECLASS="replaceable"><I>reading-tar</I></CODE> can be directory(s) or file(s).Just be sure to use<SPANCLASS="link">relative pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>that don't start with a slash - otherwise, the <CODECLASS="replaceable"><I>writing-tar</I></CODE> will write thecopies in the same place the originals came from!</P><PCLASS="para">"How about an example," you ask?<ACLASS="xref"HREF="ch18_16.htm#UPT-ART-6890-FIG-0"TITLE="Copying /home/jane to /work/bkup with tar">Figure 18.2</A>has one.It copies from the directory <EMCLASS="emphasis">/home/jane</EM>, with all its files andsubdirectories.The copy is made in the directory <EMCLASS="emphasis">/work/bkup/jane</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mkdir /work/bkup/jane</B></CODE>% <CODECLASS="userinput"><B>cd /home/jane</B></CODE>% <CODECLASS="userinput"><B>tar cf - . | (cd /work/bkup/jane && tar xBf -)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The<SPANCLASS="link"><CODECLASS="userinput"><B>&&</B></CODE> operator (<ACLASS="linkend"HREF="ch44_09.htm"TITLE="Testing Your Success ">44.9</A>)</SPAN>tells the shell to start <CODECLASS="literal">tar xBf</CODE> only if the previous command(the <CODECLASS="literal">cd</CODE>) succeeded.That prevents <EMCLASS="emphasis">tar</EM> writing files into the same directory it's readingfrom - if the destination directory isn't accessible or you flub its pathname.<ACLASS="indexterm"NAME="AUTOID-21121"></A>If your <EMCLASS="emphasis">tar</EM> has a <EMCLASS="emphasis">B</EM> (reblocking) option, use it to help be surethat the copy is made correctly.If your <EMCLASS="emphasis">tar</EM> doesn't have a reblocking option, you can use thistrick suggested by Chris Torek:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cf - . | cat | (cd /work/backup/jane && tar xbf 1 -)</B></CODE></PRE></BLOCKQUOTE></P><BLOCKQUOTECLASS="caution"><PCLASS="para"><STRONG>CAUTION:</STRONG> <ACLASS="indexterm"NAME="AUTOID-21132"></A>At least one <EMCLASS="emphasis">tar</EM> version has a <EMCLASS="emphasis">v</EM> (verbose) option that<SPANCLASS="link"> writes the verbose text to standard output instead of standard error ! (<ACLASS="linkend"HREF="ch19_08.htm"TITLE="Problems with Verbose tar ">19.8</A>)</SPAN>If your <EMCLASS="emphasis">tar</EM> does that, don't use <EMCLASS="emphasis">v</EM> on the <CODECLASS="replaceable"><I>reading-tar</I></CODE>(the <EMCLASS="emphasis">tar</EM> that feeds the pipe)-use <EMCLASS="emphasis">v</EM> on the <CODECLASS="replaceable"><I>writing-tar</I></CODE>.</P></BLOCKQUOTE><PCLASS="para">You can use other options that your <EMCLASS="emphasis">tar</EM> might have-like<SPANCLASS="link">excluding files or directories (<ACLASS="linkend"HREF="ch20_08.htm"TITLE="Telling tar Which Files to Exclude or Include ">20.8</A>)</SPAN>-on the <CODECLASS="replaceable"><I>reading-tar</I></CODE>, too.Some gotchas:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Symbolic links (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>will be copied exactly.If they point to relative pathnames, the copied links might pointto locations that don't exist.You can search for these symbolic links with<SPANCLASS="link"><EMCLASS="emphasis">find -type l</EM> (<ACLASS="linkend"HREF="ch17_13.htm"TITLE="Searching for Files by Type ">17.13</A>)</SPAN>or<SPANCLASS="link"><EMCLASS="emphasis">oldlinks</EM> (<ACLASS="linkend"HREF="ch16_28.htm"TITLE="oldlinks: Find Unconnected Symbolic Links ">16.28</A>)</SPAN>.</P></LI><LICLASS="listitem"><PCLASS="para">A<SPANCLASS="link">hard link (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>will be copied as a file.If there are more hard links to that file in the files you're copying,they will be linked to the copy of the first link.That can be good because the destination might be on a differentfilesystem (a hard link to the original file can't work then).It can be bad if the link pointed to a really big file;the copy can take a lot of disk space.You can search for these hard links by:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Searching the directory from which you're copying with<SPANCLASS="link"><EMCLASS="emphasis">find -links +1 -type f</EM> (<ACLASS="linkend"HREF="ch17_05.htm"TITLE="Searching for Old Files ">17.5</A>)</SPAN>to find all files that have more than one link, and</P></LI><LICLASS="listitem"><PCLASS="para">Running the <CODECLASS="replaceable"><I>reading-tar</I></CODE> with its <EMCLASS="emphasis">l</EM> (lowercase letter L) option to complain if it didn't copy all links to a file.</P></LI></UL></LI></UL><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-6890-FIG-0">Figure 18.2: Copying /home/jane to /work/bkup with tar</A></H4><IMGCLASS="graphic"SRC="figs/6890.gif"ALT="Figure 18.2"><PCLASS="para">If your system has<SPANCLASS="link"><EMCLASS="emphasis">rsh</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,you can run the <CODECLASS="replaceable"><I>writing-tar</I></CODE> on a remote system.For example, to copy a directory to the computer named <EMCLASS="emphasis">kumquat</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rsh kumquat mkdir /work/bkup/jane</B></CODE>% <CODECLASS="userinput"><B>tar cf - . | rsh kumquat 'cd /work/bkup/jane && tar xBf -'</B></CODE></PRE></BLOCKQUOTE></P><ACLASS="indexterm"NAME="AUTOID-21180"></A><ACLASS="indexterm"NAME="AUTOID-21181"></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_15.htm"TITLE="18.15 Copying Directory Trees with cp -r "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.15 Copying Directory Trees with cp -r "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="CHAPTER"HREF="ch19_01.htm"TITLE="19. Creating and Reading Archives"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 19. Creating and Reading Archives"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">18.15 Copying Directory Trees with cp -r </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">19. Creating and Reading Archives</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 + -