📄 548-550.html
字号:
<HTML>
<HEAD>
<TITLE>Using Linux:System Maintenance</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789716232//-->
<!--TITLE=Using Linux//-->
<!--AUTHOR=William Ball//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=31//-->
<!--PAGES=548-550//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="544-548.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="550-554.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Compressing Archive Files with <I>tar</I>
</B></FONT></P>
<P><TT>tar</TT> can also serve double duty as an archive compression/expansion utility. When used with the <TT>z</TT> option, <TT>tar</TT> <TT>gzips (or gunzips</TT>) the <TT>tar</TT> archive file it creates (or extracts) by using <TT>gzip</TT> compression. To compress our <TT>sharedfiles</TT> tree, use the following:</P>
<!-- CODE SNIP //-->
<PRE>
#tar czf meshare.tar.gz me
</PRE>
<!-- END CODE SNIP //-->
<P><TT>tar</TT> compresses and archives in one step. If you use the <TT>filename.tar.gz</TT> naming convention for gzipped <TT>tar</TT> files, it helps you keep track of when to use the <TT>z</TT> option to extract a <TT>tar</TT> file.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>SEE ALSO</B>
<DL>
<DD><B>•</B> For more information about file attributes, see page 43 and page 418.
<DD><B>•</B> For more information about command-line syntax, see page 16.
</DL>
<HR></FONT>
</BLOCKQUOTE>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>tar command line options do not require leading dashes</B></FONT>
<BR>Unlike most Linux commands, the initial arguments on the command line of a <TT>tar</TT> command do not require leading dashes to set them apart. This is partly because the first argument after the <TT>tar</TT> command must be one of the major <TT>tar</TT> functions (<TT>c, x, t, u,</TT> and so on), so the shell doesn't need an additional cue to determine how to parse the command line.</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Using <I>find</I> to Locate Files for <I>tar</I> Backups
</FONT></H4>
<P>Often you must back up certain kinds of files based on their type, owner, creation date, or other file attributes. The <TT>find</TT> command helps you look for those files by using a large number of options.</P>
<!-- CODE SNIP //-->
<PRE>
#find pathname(s) to search search rule expression
</PRE>
<!-- END CODE SNIP //-->
<P>For instance, you can use <TT>find</TT> to look for HTML files like so:</P>
<!-- CODE SNIP //-->
<PRE>
#find . -name '*.html’ -print
</PRE>
<!-- END CODE SNIP //-->
<P>Used this way, <TT>find</TT> locates all files in the current directory and any of its subdirectories with filenames ending in <TT>.html</TT>, and then prints a list of those filenames. (Actually, you would not have to list the <TT>.</TT> before <TT>html</TT> to indicate the current directory; <TT>find</TT> automatically searches the current directory if no pathname(s) are listed.)</P>
<P><TT>find</TT> works like an inspection line in a factory. It passes each filename it finds in the specified path(s) to the first rule in the search rule expression. Each rule in the search processes the file in some way, and then either passes the file to the next rule, or excludes the file from the search.</P>
<P><FONT SIZE="+1"><B>Using <I>find</I> to Help with System Maintenance
</B></FONT></P>
<P>You can use <TT>find</TT> to search using a number of different rules. When using <TT>find</TT> for system maintenance purposes, often you will need to locate files based on one of three criteria: file ownership, file size, or other file attributes (such as file access time, file type, or file permissions). <TT>find</TT> is equipped to handle most jobs simply. For instance, you can print a list of all files of type <TT>d</TT> (the directories) in <TT>/usr</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
#find /usr -type d -print
#find $HOME -size +5k -print
</PRE>
<!-- END CODE SNIP //-->
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>find uses three general kinds of search rules</B></FONT>
<BR>Find recognizes three basic kinds of rules: tests, actions, and options. <I>Tests</I>, such as <TT><B>name</B></TT>, run tests on each file they process, and forward only those files that pass the test to the next rule in the search. <I>Actions</I>, like <TT><B>print</B></TT>, perform some action on all the files that reach them; they also can pass the file to the next rule in the search. <TT><B>find</B></TT> <I>options</I> determine how the command runs generally; they don’t process individual filenames.</TABLE>
<P>In the preceding example, <TT>find</TT> prints a list of files with sizes larger than 5KB in your home directory. (In <TT>find</TT> test expressions, <TT>+number</TT> represents all values greater than the number, <TT>-number</TT> represents all values less than the number, and a number with no plus or minus attached stands for the value of the number itself.)</P>
<P>Using the <TT>atime</TT> and <TT>fprint</TT> options, you can print a list of man page files that have not been accessed within the last five days in the log file <TT>unhit_man_files.log</TT>.</P>
<!-- CODE SNIP //-->
<PRE>
#find /usr/man -atime +5 -fprint unhit_man_files.log
</PRE>
<!-- END CODE SNIP //-->
<P><TT>find</TT> can use a version of the <TT>ls</TT> command as an option. The following command prints a list of files on the current device (<TT>-xdev</TT>) belonging to the user <TT>tb</TT> in the <TT>ls -dils</TT> format to the file <TT>all_tb_files.lst</TT>.</P>
<!-- CODE SNIP //-->
<PRE>
#find / -xdev -user tb -ls > all_tb_files.lst
</PRE>
<!-- END CODE SNIP //-->
<P>This last example illustrates a way to use <TT>find</TT> with <TT>tar</TT>. If the user <TT>tb</TT> were taking a sabbatical, you could round up all the files owned by that user and back them up by using the <TT>tar</TT> command.</P>
<!-- CODE //-->
<PRE>
#cd /
#find / -user tb -print
/usr/local/proj1/stadd.o
/usr/local/proj1/stadd.c
/home/tb/docfiles/mk2efs_notes
/home/tb/scripts/perlscripts/hform1
...
#find / -xdev -user tb -fprint all_tb_files.lst
#tar -cvz -T all_tb_files.lst -f all_tb_files.tar.gz
</PRE>
<!-- END CODE //-->
<P>The <TT>-T</TT> option instructs <TT>tar</TT> to look for the list of files to archive inside a file, instead of on the command line.(When using the <TT>-T</TT> option with <TT>tar</TT>, you must separate command-line options with leading dashes.) If you were to delete all the <TT>tb</TT> files, and needed to reinstall them when <TT>tb</TT> returned, you would have to extract the <TT>all_tb_files.tar.gz</TT> archive from the same spot you created it (to ensure that the <TT>tar</TT> extraction replaced the files in the original spots recorded by the <TT>find</TT> command).</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="544-548.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="550-554.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -