The find command in the shell

Source: Internet
Author: User

Basic composition:

find pathname-option [-print-exec-ok ...]

Classify the attributes of the memory parameters ~

Pathname: Specifies the query root path of the find command, such as '/', './'.

You can also specify the search path in the-path parameter.

-option:

1. File Operation:

         -name Enclose the file name of the query in quotation marks, which can be applied to a simple regular expression (which should be changed to the Shell's meta-character, the same as below).

        -type Specifies the type of file to find

B-block device files.              D-Directory.              C-character device file.              P-Pipeline file.              L-Symbolic link file. F-Normal file.

        -size N Specifies the file size, if the number before + means greater than,-indicates less than; common units C (bytes), K (KB), M (MB)

2. User rights:

        -perm User rights permission, with commonly used three digits, such as 644 to indicate permissions. If the number before + represents at least one of the specified access rights,-indicates that all access rights specified must be included

         -user/-nouser by file owner query/Find files without valid owner

       -group/-nogroup search/Find documents without valid ownership rent by file

3. Time option:

        -atime/-amin n recently accessed (accessed) file, the former represents the unit is day (24h), the latter unit is minutes (minute); if the number before + means n time,-Indicates the last n time. After the same.

         -ctime/-cmin n Recent state change (changed) files

        -mtime/-mmin n Recent content modified (modified) files

       -newer file! Otherfile Find change times than file New files, plus non (! ) to find older files than Otherfile

(Note that the logical symbols are included!) (not),-a (and),-O (or), in the command are short-circuit evaluation, simply said that if the following parameters do not affect the logical judgment, then the subsequent evaluation is not. In the Find command, the logical symbol is usually added before the option)

4. File path:

       -follow encounters a symbolic link file, it tracks the file to which the link is pointing

         -mount does not cross the mount point of the file system, that is, in the current file system query, does not enter other file systems (such as mounted Windows systems)

        -path gives the file path, which can be queried in the path to satisfy simple regular Expressions (metacharacters). For example, find the ' index.html ' file in the './var/www ' subdirectory of the current path:

Find ' ./var/www* ' ' index.html ' -print
./var/www/learn/study_smarty/templates/Index.tpl. /var/www/learn/study_smarty/docs/index.php. /var/www/learn/index.php. /var/www/index.html

(as you can see in the results, the parameters in the-path option are ambiguous query conditions, and I understand that the-path option finds the file name is actually a string with a path, whereas-path is the preceding path character in the result string.) )

         -prune Indicates the directory to ignore. Combine the-path option, such as finding the ' index.html ' file in the current directory, ignoring the '/www/bin ' directory under the path:

Find ' ./www/bin ' ' index.html ' -print
' index.html ' ' index.html The shorthand expression for '-print ' . evaluated sequentially,-A and-O are short-circuit evaluated, with Shell's && and | | Similarly, if-path "./www/bin" is true, the evaluation is-prune,-prune returns True, "and" The logical expression is true, otherwise no value-prune, and the logical expression is false. If the-path "/usr/sam"-a-prune is false, then the value is evaluated-name. -name returns True, or the logical expression is true, otherwise no value is evaluated-name, or the logical expression is true.

(therefore, the other query criteria must be placed after-O!) And the-path option is usually put in the front,-prune and-O also should be in conjunction with)

Avoiding multiple files can be used:

find /usr  "index.html" -print

' \ (' and ' \ ') ' means escape, and the shell no longer has a special explanation for the parentheses, and the enclosed parentheses indicate a combination.

(Note that the path cannot be added by the '/' number!) )

        -depth matches all of the files before looking in subdirectories. That is, breadth traversal query. Note The-prune option fails after the-DEPTH option is added.

         -maxdepth n Sets the directory level for recursive search, 1 for the current directory, i.e. no recursive search.

5. Query execution:

        -exec Command {} \; Executes the command command on the query's resulting file, which contains the results of the query.

        -ok Command {} \; As with-exec, it is a relatively safe practice to have interactive prompts for execution when executing command commands.

When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there are too many processes and degraded system performance, and therefore inefficient. Turn

         Xargs with the pipe to perform the operation on the matching file.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on. With the Xargs command, there is only one process. Turn

For example, search for ' admin ' in a normal file under the current path:

Find Xargs grep ' Admin '

For a command of a parameter, Xargs implicitly passes the matching file, such as the above grep files, and implicitly passes the result to files. As a multi-parameter command, such as CP file Dir, use the-i option of the Xargs, for example to copy the query price to '/usr ':

Find Xargs CP {}/usr/

With the-I option, Xargs passes the matching results to {}, which facilitates the use of multi-parameter commands.

The find command in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.