📄 329-331.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Managing Files and Directories</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=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=17//-->
<!--PAGES=329-331//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="327-329.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="331-334.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>In Table 17.5, <I>size</I> is a number and also may be <TT>C</TT> for <TT>sizeof(char)</TT>, <TT>S</TT> for <TT>sizeof(short)</TT>, <TT>I</TT> for <TT>sizeof(int)</TT>, or <TT>L</TT> for <TT>sizeof(long)</TT>. If <I>type</I> is <TT>f</TT>, <I>size</I> may also be <TT>F</TT> for <TT>sizeof(float)</TT>, <TT>D</TT> for <TT>sizeof(double)</TT>, or <TT>L</TT> for <TT>sizeof(long double)</TT>.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>sizeof is a C language function that returns the number of bytes in the data structure passed as the parameter. For example, you would use the following function call to determine the number of bytes in an integer on your system, because the number of bytes in an integer is system-dependent:<!-- CODE SNIP //-->
<PRE>
sizeof( int );
</PRE>
<!-- END CODE SNIP //-->
<HR></FONT>
</BLOCKQUOTE>
<P><I>radix</I> in Table 17.3 stands for number system and is <TT>d</TT> for decimal, <TT>o</TT> for octal, <TT>x</TT> for hexadecimal, or <TT>n</TT> for none. <I>bytes</I> is hexadecimal with a prefix of <TT>0x</TT> or <TT>0X</TT>; it’s multiplied by 512 with a <TT>b</TT> suffix, by 1,024 with <TT>k</TT>, and by 1,048,576 with an <TT>m</TT> suffix. <TT>-s</TT> without a number implies 3; <TT>-w</TT> without a number implies 32. By default, <TT>od</TT> uses <TT>-A o -t d2 -w 16</TT>.</P>
<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Searching for Files</FONT></H3>
<P>If you can’t find a file by looking with the <TT>ls</TT> command, you can use the <TT>find</TT> command. The <TT>find</TT> command is an extremely powerful tool, which makes it one of the more difficult commands to use. The <TT>find</TT> command has three parts, each of which can consist of multiple subparts:</P>
<DL>
<DD><B>•</B> Where to look
<DD><B>•</B> What to look for
<DD><B>•</B> What to do when you find it
</DL>
<P>If you know the name of a file but don’t know where in the Linux file structure it’s located, the simplest case of the <TT>find</TT> command works like this:</P>
<!-- CODE SNIP //-->
<PRE>
find / -name filename -print
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>Be careful when searching from the root directory; on large systems, it can take a long time to search every directory beginning with the root directory and continuing through every subdirectory and disk (and remotely mounted disk) to find what you’re looking for.<HR></FONT>
</BLOCKQUOTE>
<P>It may be more prudent to limit the search to one or two directories, at most. For example, if you know that a file is probably in the /usr or /usr2 directory, use the following command instead:
</P>
<!-- CODE SNIP //-->
<PRE>
find /usr /usr2 -name <I>filename</I> -print
</PRE>
<!-- END CODE SNIP //-->
<P>You can use many different options with <TT>find</TT>; Table 17.6 lists just a few. To see all the available options, use the <TT>man find</TT> command.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 17.6</B> A Sample of the <TT>find</TT> Command Flags
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Command
<TH WIDTH="80%" ALIGN="LEFT">Description
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP"><TT>-name <I>file</I></TT>
<TD>The <I>file</I> variable can be the name of a file or a wildcarded filename. If it’s a wildcarded filename, every file that matches the wildcards is selected for processing.
<TR>
<TD VALIGN="TOP"><TT>-links <I>n</I></TT>
<TD>Any file that has <I>n</I> or more links to it is selected for processing. Replace <I>n</I> with the number you want to check.
<TR>
<TD VALIGN="TOP"><TT>-size <I>n</I>[c]</TT>
<TD>Any file that occupies <I>n</I> or more 512-byte blocks is selected for processing. A <TT>c</TT> appended to <I>n</I> means to select any file that occupies <I>n</I> or more characters.
<TR>
<TD VALIGN="TOP"><TT>-atime <I>n</I></TT>
<TD>Select any file that has been accessed in the past <I>n</I> days. Note that the act of looking for a file with <TT>find</TT> modifies the access date stamp.
<TR>
<TD VALIGN="TOP"><TT>-exec <I>cmd</I></TT>
<TD>After you select a list of files, you can run a Linux command that uses the selected files as an argument. You use two simple rules with <TT>-exec</TT>: the name of a selected file is represented by <TT>{}</TT>, and the command must be terminated by an escaped semicolon, which is represented by <TT>\</TT>;. Suppose you create a user directory while logged in as root. As a result, all the files are owned by root, but the files should be owned by the user. You would issue the following command to change the owner of all the files in /home/jack and all subdirectories from root to jack:
<TT>find /home/jack -exec chown jack {} \;</TT>
<TR>
<TD VALIGN="TOP"><TT>-print</TT>
<TD>This instruction, the most often used, simply prints the name and location of any selected files.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The <TT>find</TT> command allows you to perform many logical tests on files as well. For example, if you want to find a selection of filenames that can’t be collectively represented with wildcards, you can use the <I>or</I> option (<TT>-o</TT>) to obtain a list:</P>
<!-- CODE SNIP //-->
<PRE>
find /home ( -name <I>file1</I> -o -name <I>file2</I> ) -print
</PRE>
<!-- END CODE SNIP //-->
<P>You can combine as much selection criteria as you want with the <TT>find</TT> command. Unless you specify the <TT>-o</TT> option, <TT>find</TT> assumes you mean <I>and</I>. For example, the command <TT>find -size 100 -atime 2</TT> means find a file that’s at least 100 blocks in size and that was last accessed at least two days ago. You can use parentheses, as in the above example, to prevent ambiguous processing of your criteria, especially if you combine an and/or selection criteria.</P>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Changing File Time and Date Stamps</FONT></H3>
<P>Each Linux file maintains three time and date stamps: the date of the file’s creation, the date of the file’s last modification, and the date of the last access. The file creation date can’t be changed artificially except by deliberately copying and renaming a file. Whenever a file is read or opened by a program, the file’s access date stamp is modified. As mentioned in the preceding section, using the <TT>find</TT> command also causes the access date to be modified.</P>
<P>If a file is modified in any way—that is, if it’s written to, even if the file is actually not modified—the file modification and file access date stamps are updated. The date stamps on a file are useful if you need to back up selectively only files that have been modified since a given date. You can use the <TT>find</TT> command for this purpose.</P>
<P>If you want to modify the date stamps on a file without actually modifying the file, you can do so with the <TT>touch</TT> command. By default, <TT>touch</TT> updates the access and modification date stamps on a file with the current system date. By default, if you attempt to touch a file that doesn’t exist, <TT>touch</TT> creates the file.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="327-329.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="331-334.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 + -