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

📄 ch20_08.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 20] 20.8 Telling tar Which Files to Exclude or Include </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-10-23T15:44:58Z"><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="ch20_01.htm"TITLE="20. Backing Up Files"><LINKREL="prev"HREF="ch20_07.htm"TITLE="20.7 Creating a Timestamp File for Selective Backups "><LINKREL="next"HREF="ch20_09.htm"TITLE="20.9 When a Program Doesn't Understand Wildcards "></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="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"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 20<BR>Backing Up Files</FONT></B></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></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0424">20.8 Telling tar Which Files to Exclude or Include </A></H2><PCLASS="para">[This article was written for SunOS.Many versions of tar don't have some or all ofthese features.Some do it in a different way.Check your tar manual page, or use the<SPANCLASS="link"><EMCLASS="emphasis">GNU </EM>tar (<ACLASS="linkend"HREF="ch19_06.htm"TITLE="GNU tar Sampler ">19.6</A>)</SPAN>that we provideon the disc. -JP]</P><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-424-IX-ARCHIVES-TAR-ARCHIVES"></A><ACLASS="indexterm"NAME="UPT-ART-424-IX-TAR-ARCHIVES-EXCLUDING-INCLUDING-FILES-IN"></A>On some systems,<SPANCLASS="link"><EMCLASS="emphasis">make</EM> (<ACLASS="linkend"HREF="ch28_13.htm"TITLE="make Isn't Just for Programmers! ">28.13</A>)</SPAN>creates filenames starting with acomma&nbsp;(<CODECLASS="literal">,</CODE>) to keep track of dependencies.Various editors create backup files whose names end with apercent sign&nbsp;(<CODECLASS="literal">%</CODE>) or atilde&nbsp;(<CODECLASS="literal">~</CODE>).I often keep the original copy of a program with the <EMCLASS="emphasis">.orig</EM>extension and old versions with a <EMCLASS="emphasis">.old</EM>extension.</P><PCLASS="para">I often don't want to save these files on my backups.There may be some binary files that I don'twant to archive, but don't want to delete either.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22299"></A><ACLASS="indexterm"NAME="AUTOID-22302"></A><ACLASS="indexterm"NAME="AUTOID-22305"></A>A solution is to use the<EMCLASS="emphasis">X</EM>flag to<SPANCLASS="link"><EMCLASS="emphasis">tar</EM> (<ACLASS="linkend"HREF="ch20_01.htm#UPT-ART-8024"TITLE="tar in a Nutshell ">20.1</A>)</SPAN>.[Check your <EMCLASS="emphasis">tar</EM> manual page for the <EMCLASS="emphasis">F</EM> and <EMCLASS="emphasis">FF</EM> options, too.<EMCLASS="emphasis">-JIK</EM>&nbsp;]This flag specifies that the matching argument to <EMCLASS="emphasis">tar</EM>is the name of a file that lists files to exclude from the archive.Here is an example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find project ! -type d -print | \egrep '/,|%$|~$|\.old$|SCCS|/core$|\.o$|\.orig$' &gt; Exclude</B></CODE>% <CODECLASS="userinput"><B>tar cvfX project.tar Exclude project</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In this example,<SPANCLASS="link"><EMCLASS="emphasis">find</EM> (<ACLASS="linkend"HREF="ch17_01.htm#UPT-ART-7410"TITLE="The find Command Is Great; The Problem Is Finding How to Use It">17.1</A>)</SPAN>lists all files in the directories, but does notprint the directory names explicitly.If you have a directory name in an excluded list, it will also exclude all the files inside the directory.<SPANCLASS="link"><EMCLASS="emphasis">egrep</EM> (<ACLASS="linkend"HREF="ch27_05.htm"TITLE="Extended Searching for Text with egrep ">27.5</A>)</SPAN>is then used as a filter to exclude certain files from the archive.Here,<EMCLASS="emphasis">egrep</EM>is given several regular expressions to match certain files.This expression seems complex but is simple once you understanda few special characters:</P><DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="literal">/</CODE></DT><DDCLASS="listitem"><PCLASS="para">The slash is not a special character. However, since no filenamecan contain a slash, it matches the beginning of a filename,as output by the <EMCLASS="emphasis">find</EM>command.</P></DD><DTCLASS="term"><CODECLASS="literal">|</CODE></DT><DDCLASS="listitem"><PCLASS="para">The vertical bar separates each regular expression.</P></DD><DTCLASS="term"><CODECLASS="literal">$</CODE></DT><DDCLASS="listitem"><PCLASS="para">The dollar sign is one of the two regular expression&quot;anchors&quot;and specifies the end of the line, or filename in this case.The other anchor, which specifies the beginning of the line, is<CODECLASS="literal">^</CODE> (caret).But because we are matching filenames output by<EMCLASS="emphasis">find</EM>,the only filenames that can match<CODECLASS="literal">^</CODE>are those in the top directory.</P></DD><DTCLASS="term"><CODECLASS="literal">\.</CODE></DT><DDCLASS="listitem"><PCLASS="para">Normally the dot matches any character in a regularexpression. Here, we want to match the actualcharacter <CODECLASS="literal">.</CODE> (dot),which is why the backslash is used toquoteorescapethe normal meaning.</P></DD></DL><PCLASS="para">A breakdown of the patterns and examples of the filesthat match these patterns is given here:</P><TABLECLASS="informaltable"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Pattern</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Matches Files</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Used by</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">/,</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">starting with <EMCLASS="emphasis">,</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">make</EM> dependency files</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">%$</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">ending with <EMCLASS="emphasis">%</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">textedit</EM> backup files</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">~$</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">ending with <EMCLASS="emphasis">~</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">emacs</EM> backup files</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">\.old$</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">ending with <EMCLASS="emphasis">.old</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">old copies</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">SCCS</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">in <EMCLASS="emphasis">SCCS</EM> directory</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><SPANCLASS="link">Source Code Control System (<ACLASS="linkend"HREF="ch20_13.htm"TITLE="SCCS Basics ">20.13</A>)</SPAN></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">/core$</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">with name of <EMCLASS="emphasis">core</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><SPANCLASS="link">core dump (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">\.o$</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">ending with <EMCLASS="emphasis">.o</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">object files</TD></TR><TRCLASS="row"VALIGN="TOP"><TD

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -