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

📄 unx04.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 4 页
字号:

<PRE>$ find /usr/home -user me -size +50 -print

/usr/home/stuff/bigfile

/usr/home/trash/bigfile.old</PRE>

<P>first finds all the files beginning at /usr/home that are owned by me and are larger than 50 blocks. Then it prints the full path name. (Actually, the full path name of the found files is sent to the standard output file, which is discussed later in 
this chapter.)

<BR></P>

<P>The argument expressions for the find command fall into three categories:

<BR></P>

<UL>

<LI>Search criteria

<BR>

<BR></LI>

<LI>Action expressions

<BR>

<BR></LI>

<LI>Search qualifiers

<BR>

<BR></LI></UL>

<P>Although the three types of expressions have different functions, each is still considered a Boolean expression and must be found to be true before any further evaluation of the entire expression can take place. (The significance of this is discussed 
later.) Typically, a find operation consists of one or more search criteria, a single action expression, and perhaps a search qualifier. In other words, it finds a file and takes some action, even if that action is simply to print the path name. The rest 
of this section describes each of the categories of the find options.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I16" NAME="I16">

<FONT SIZE=3><B>Search Criteria</B>

<BR></FONT></A></CENTER></H4>

<P>The first task of the find command is to locate files according to some user-specified criteria. You can search for files by name, file size, file ownership, and several other characteristics.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I17" NAME="I17">

<FONT SIZE=3><B>Finding Files with a Specific Name: </B><B><I>-name fname</I></B>

<BR></FONT></A></CENTER></H5>

<P>Often, the one thing that you know about a file for which you're searching is its name. Suppose that you wanted to locate&#151;and possibly take some action on&#151;all the files named core. You might use the following command:

<BR></P>

<PRE>$ find / -name core -print</PRE>

<P>This locates all the files on the system that exactly match the name core, and it prints their complete path names.

<BR></P>

<P>The -name option makes filename substitutions. The command

<BR></P>

<PRE>$ find /usr/home -name &quot;*.tmp&quot; -print</PRE>

<P>prints the names of all the files that end in .tmp. Notice that when filename substitutions are used, the substitution string is enclosed in quotation marks. This is because the UNIX shell attempts to make filename substitutions before it invokes the 
command. If the quotation marks were omitted from &quot;*.tmp&quot; and if the working directory contained more than one *.tmp file, the actual argument passed to the find command might look like this:

<BR></P>

<PRE>$ find /usr/home -name a.tmp b.tmp c.tmp -print</PRE>

<P>This would cause a syntax error to occur.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I18" NAME="I18">

<FONT SIZE=3><B>Locating Files of a Specific Size: </B><B><I>-size n</I></B>

<BR></FONT></A></CENTER></H5>

<P>Another useful feature of find is that it can locate files of a specific size. The -size n expression is a good example of a search criterion that is evaluated numerically. The numeric portion of the expression may be integers in the form n, -n, or +n. 

An integer without a sign matches if the file is exactly n. An integer preceded by a minus sign matches if the requested file is smaller than n. An integer preceded by a plus sign matches if the file is larger than n. For example,

<BR></P>

<PRE>$ find / -size +100 -print</PRE>

<P>prints the names of all the files that are more than 100 blocks long.

<BR></P>

<P>In the -size expression, the integer may be suffixed with the character c. With the suffix, the match is made on the file size in characters. Without the suffix, the match is made on the file size in blocks. Therefore, the command

<BR></P>

<PRE>$ find / -size -512c -print</PRE>

<P>prints the names of all the files that are less than 512 bytes long.

<BR></P>

<P>Other search criteria include:

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>-user uname</P>

<TD>

<P>Looks for files that are owned by the user with the login name of uname. If uname is numeric it is compared to the user number.</P>

<TR>

<TD>

<P>-group gname</P>

<TD>

<P>Looks for files that are owned by a member of the group gname. If gname is numeric, it is compared to the group number.</P>

<TR>

<TD>

<P>-atime n</P>

<TD>

<P>Looks for files that were last accessed n days ago. n must be an integer. It can take the form n, -n, or +n.</P>

<TR>

<TD>

<P>-mtime n</P>

<TD>

<P>Looks for files that were last modified n days ago.  n must be an integer.</P>

<TR>

<TD>

<P>-perm onum</P>

<TD>

<P>Looks for files whose permission flags match the octal number onum. If onum is preceded by a minus sign, the match will be made if the permission flag has the bit(s) set that matches the bit(s) in onum. For example, the expression -perm -100 will be 
true for any file that is executable by its owner.</P>

<TR>

<TD>

<P>-links n</P>

<TD>

<P>A match if the file has n links. n must be an integer. It can take the form n, -n, or +n.</P>

<TR>

<TD>

<P>-type <I>x</I></P>

<TD>

<P>Looks for files that are of type <I>x</I>. Valid values for <I>x</I> are: b for a block special file, c for a character special file, d for a directory, p for a fifo (named pipe), and f for an ordinary file.</P>

<TR>

<TD>

<P>-newer fname</P>

<TD>

<P>Looks for files that have been modified more recently than the file fname.</P>

<TR>

<TD>

<P>-local</P>

<TD>

<P>Looks for files that reside on the local system as opposed to a remote site.</P></TABLE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I19" NAME="I19">

<FONT SIZE=3><B>Locating Files of a Specific Size: </B><B><I>-size n</I></B>

<BR></FONT></A></CENTER></H5>

<P>Once the find command has located a file, it must be told what to do with it. These are called action expressions.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I20" NAME="I20">

<FONT SIZE=3><B>Displaying the Path Names of Found Files: </B><B><I>-print</I></B>

<BR></FONT></A></CENTER></H5>

<P>As you know, it does little good to locate a file, and then take no action. One commonly used action is the print expression, which causes the complete path name to be printed when a file is found. This is useful if you want to check for the existence 
of a file before deciding to take some other action.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I21" NAME="I21">

<FONT SIZE=3><B>Executing a UNIX Command on the Found Files: </B><B><I>-exec cmd \;</I></B>

<BR></FONT></A></CENTER></H5>

<P>Sometimes you know what action you want to take once you find a file. In those cases, you can use the expression

<BR></P>

<PRE>exec <I>cmd</I> \;</PRE>

<P>where <I>cmd</I> is any UNIX command. \; tells the find command to take the action specified between exec and \;. find then continues to evaluate argument expressions.

<BR></P>

<P>The most powerful aspect of the find command is the unique file substitution method found within the exec cmd expression. In any cmd statement, the argument {} is replaced with the name of the currently matched file. For example, suppose that the 
command

<BR></P>

<PRE>$<B> </B>find /usr/home -name core -print</PRE>

<P>gives the following results:

<BR></P>

<PRE>/usr/home/dave/core

/usr/home/marsha/core

/usr/home/mike/core</PRE>

<P>The command

<BR></P>

<PRE>$<B> </B>find /usr/home -name core -exec rm {} \;</PRE>

<P>has the same effect as issuing these commands:

<BR></P>

<PRE>$ rm /usr/home/dave/core

$ rm /usr/home/mike/core

$ rm /usr/home/marsha/core</PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I22" NAME="I22">

<FONT SIZE=3><B>Executing a UNIX Command on Found Files, But Querying First: </B><B><I>-ok cmd \;</I></B>

<BR></FONT></A></CENTER></H5>

<P>The -ok expression works exactly like the -exec expression, except that the execution of the command is optional. When it encounters an ok expression, the find program displays the generated command, with all substitutions made, and prints a question 
mark. If the user types y, the command is executed.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I23" NAME="I23">

<FONT SIZE=3><B>Writing Found Files to a Device: </B><B><I>-cpio device</I></B>

<BR></FONT></A></CENTER></H5>

<P>The -cpio device action expression causes a file to be written to a given device in cpio form. For example, the command

<BR></P>

<PRE>$ find /usr/home -cpio -o &gt;/dev/rmt0</PRE>

<P>writes all the files in /usr/home and all its subdirectories to the magnetic tape device /dev/rmt0. This is a good way to back up data files. It is a shorthand equivalent of

<BR></P>

<PRE>$<B> </B>find /usr/home -print | cpio &gt;/dev/rmt0</PRE>

<H4 ALIGN="CENTER">

<CENTER><A ID="I24" NAME="I24">

<FONT SIZE=3><B>Search Qualifiers</B>

<BR></FONT></A></CENTER></H4>

<P>There are times when you may want the find command to alter its normal search path. This is accomplished by adding search qualifiers to the find command.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I25" NAME="I25">

<FONT SIZE=3><B>Searching for Files on Only the Current File System: </B><B><I>-mount</I></B>

<BR></FONT></A></CENTER></H5>

<P>The -mount search qualifier restricts the search to the file system named in the starting point. For example, the command

<BR></P>

<PRE>$ find / -mount -type d -print</PRE>

<P>prints the names of all the directories in only the root file system.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I26" NAME="I26">

<FONT SIZE=3><B>Altering the Search Path with </B><B><I>-depth</I></B>

<BR></FONT></A></CENTER></H5>

<P>The -depth search qualifier alters the seek order to a depth-first search. The find command processes the files in a directory before it processes the directory itself. This helps in finding files to which the user has access, even if his access to the 

directory is restricted. To see the difference, try the following two commands. Remember that -print is always true.

<BR></P>

<PRE>$ find /usr -print

$ find /usr -depth -print</PRE>

<H4 ALIGN="CENTER">

<CENTER><A ID="I27" NAME="I27">

<FONT SIZE=3><B>Combining Search Criteria</B>

<BR></FONT></A></CENTER></H4>

<P>You can combine search criteria in a single command. Because the expressions in a find command are evaluated from left to right and the search fails when any one expression fails, the effect is a logical AND. For example, the command

<BR></P>

<PRE>$<B> </B>find /usr/home -name &quot;*.tmp&quot; -atime +7 -exec rm {} \;</PRE>

<P>removes all the files that end in .tmp and that have not been accessed in the last 7 days.

<BR></P>

<P>Suppose, though, that you wanted to locate files ending in either .tmp or .temp. You could use the expression -name &quot;*mp&quot;, but you might find files that you didn't expect. The solution is to combine search criteria in a logical OR expression. 

The syntax is

<BR></P>

<PRE>\( expression -o expression \)</PRE>

<P>The \ in front of the parentheses is an escape character; it prevents the shell from misinterpreting the parentheses. The following command line, for example, finds files ending in either .tmp or .temp:

<BR></P>

<PRE>$ find /usr/home \( -name &quot;*.tmp&quot; -o -name &quot;*.temp&quot; \)</PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I28" NAME="I28">

<FONT SIZE=3><B>Negating Expressions to Find Files That Don't Meet Criteria</B>

<BR></FONT></A></CENTER></H5>

<P>Suppose that Marsha wanted to see whether anyone was putting files into her personal directory. She could use the negation operator (!), as in

<BR></P>

<PRE>$ find /usr/home/marsha ! -user marsha -print

$ /usr/home/marsha/daves.todo</PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I29" NAME="I29">

<FONT SIZE=3><B>Specifying More Than One Path to Search</B>

<BR></FONT></A></CENTER></H5>

<P>By specifying a directory in which the find command should begin searching, you can control the scope of the search. The find command actually takes a list of directories to be searched, but you must specify all paths before you supply any expression 

⌨️ 快捷键说明

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