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

📄 ch20_08.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
CLASS="entry"ROWSPAN="1"COLSPAN="1">\.orig$</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">ending with <EMCLASS="emphasis">.orig</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">original version</TD></TR></TBODY></TABLE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22407"></A>Instead of specifying which files are to be excluded, you can specify which files to archive using the<EMCLASS="emphasis">-I</EM> option.As with the exclude flag, specifying a directory tells<EMCLASS="emphasis">tar</EM>to include (or exclude) the entire directory.You should also note that the syntax of the<EMCLASS="emphasis">-I</EM>option is different from the typical<EMCLASS="emphasis">tar</EM>flag.The next example archives all C files and makefiles.It uses <EMCLASS="emphasis">egrep</EM>'s <CODECLASS="literal">()</CODE> grouping operators to makethe <CODECLASS="literal">$</CODE> anchor character apply to all patterns inside theparentheses:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find project -type f -print | \egrep '(\.[ch]|[Mm]akefile)$' &gt; Include</B></CODE>% <CODECLASS="userinput"><B>tar cvf project.tar -I Include</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">I suggest using<EMCLASS="emphasis">find</EM>to create the include or exclude file.You can edit it afterward, if you wish.One caution: extra spaces at the end of any line will cause that fileto be ignored.</P><PCLASS="para">One way to debug theoutputof the<EMCLASS="emphasis">find</EM>command is to use<SPANCLASS="link"><EMCLASS="emphasis">/dev/null</EM> (<ACLASS="linkend"HREF="ch13_14.htm"TITLE="What Can You Do with an Empty File? ">13.14</A>)</SPAN>as the output file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cvfX /dev/null Exclude project</B></CODE></PRE></BLOCKQUOTE></P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-424-SECT-1.1">20.8.1 Including Other Directories </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22433"></A><ACLASS="indexterm"NAME="AUTOID-22436"></A>There are times when you want to make an archive of several directories.You may want to archive a source directory andanother directory like<EMCLASS="emphasis">/usr/local</EM>.The natural, but wrong, way to do this is to use the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cvf /dev/rmt8 project /usr/local</B></CODE></PRE></BLOCKQUOTE></P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> When using <EMCLASS="emphasis">tar</EM>, you must never specify a directory name starting with aslash (/).This will cause problems when you restore a directory,<SPANCLASS="link">as you will see later (<ACLASS="linkend"HREF="ch20_10.htm"TITLE="Avoid Absolute Paths with tar ">20.10</A>)</SPAN>.</P></BLOCKQUOTE><PCLASS="para">The proper way to handle the incorrect example above is to use the<ACLASS="indexterm"NAME="AUTOID-22448"></A><EMCLASS="emphasis">-C</EM> flag:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cvf /dev/rmt8 project -C /usr local</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This will archive<EMCLASS="emphasis">/usr/local/...</EM>as<EMCLASS="emphasis">local/...</EM>.Article<ACLASS="xref"HREF="ch20_10.htm"TITLE="Avoid Absolute Paths with tar ">20.10</A>has more information.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-424-SECT-1.2">20.8.2 Type Pathnames Exactly </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22462"></A>For the above options to work when you extract files from an archive,the pathname given in the include or exclude filemust exactly match the pathname on the tape.</P><PCLASS="para">Here's a sample run.I'm extracting from a file named <EMCLASS="emphasis">appe.tar</EM>.Of course, this example applies to tapes, too:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar tf appe.tar</B></CODE>appecode/appendix/font_styles.ccode/appendix/xmemo.ccode/appendix/xshowbitmap.ccode/appendix/zcard.ccode/appendix/zcard.icon</PRE></BLOCKQUOTE></P><PCLASS="para">Next, I create an exclude file, named <EMCLASS="emphasis">exclude</EM>, that contains thelines:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">code/appendix/zcard.ccode/appendix/zcard.icon</PRE></BLOCKQUOTE></P><PCLASS="para">Now, I run the following <EMCLASS="emphasis">tar</EM> command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar xvfX appe.tar exclude</B></CODE>x appe, 6421 bytes, 13 tape blocksx code/appendix/font_styles.c, 3457 bytes, 7 tape blocksx code/appendix/xmemo.c, 10920 bytes, 22 tape blocksx code/appendix/xshowbitmap.c, 20906 bytes, 41 tape blockscode/appendix/zcard.c excludedcode/appendix/zcard.icon excluded</PRE></BLOCKQUOTE></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-424-SECT-1.3">20.8.3 Exclude the Archive File! </A></H3><PCLASS="para">If you're archiving the current directory (<CODECLASS="literal">.</CODE>) instead ofstarting at a subdirectory, remember to start withtwo pathnames in the <EMCLASS="emphasis">Exclude</EM> file:the archive that <EMCLASS="emphasis">tar</EM> creates and the <EMCLASS="emphasis">Exclude</EM> file itself.That keeps <EMCLASS="emphasis">tar</EM> from trying to archive its own output!</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat &gt; Exclude./somedir.tar./Exclude</B></CODE><KBDCLASS="keycap">[CTRL-d]</KBD>% <CODECLASS="userinput"><B>find . -type f -print | \egrep  '/,|%$|~$|\.old$|SCCS|/core$|\.o$|\.orig$' &gt;&gt;Exclude</B></CODE>% <CODECLASS="userinput"><B>tar cvfX somedir.tar Exclude .</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In that example,we used<SPANCLASS="link"><CODECLASS="literal">cat</CODE>&nbsp;<CODECLASS="literal">&gt;</CODE> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>to create the file quickly; you could use a texteditor instead.Notice that the pathnames in the <EMCLASS="emphasis">Exclude</EM> file start with <CODECLASS="literal">./</CODE>;that's what the <EMCLASS="emphasis">tar</EM> command expects when you tell it to archivethe current directory (<CODECLASS="literal">.</CODE>).The long <EMCLASS="emphasis">find</EM>/<EMCLASS="emphasis">egrep</EM> command line uses the<SPANCLASS="link"><CODECLASS="literal">&gt;&gt;</CODE> operator (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>to add other pathnames to the end of the <EMCLASS="emphasis">Exclude</EM> file.</P><PCLASS="para">&#13;Or, instead of adding the archive and exclude file's pathnames to theexclude file, you can move those two files somewhere out of thedirectory tree that <EMCLASS="emphasis">tar</EM> will read.<ACLASS="indexterm"NAME="AUTOID-22508"></A><ACLASS="indexterm"NAME="AUTOID-22509"></A></P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">BB, TOR</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="ch20_07.htm"TITLE="20.7 Creating a Timestamp File for Selective Backups "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 20.7 Creating a Timestamp File for Selective Backups "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="ch20_09.htm"TITLE="20.9 When a Program Doesn't Understand Wildcards "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 20.9 When a Program Doesn't Understand Wildcards "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">20.7 Creating a Timestamp File for Selective Backups </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">20.9 When a Program Doesn't Understand Wildcards </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 + -