Let's take a look at the problem analysis encountered in the use of find -- find.

Source: Internet
Author: User

Let's take a look at the problem analysis encountered in the use of find -- find.
1 simple find Command 1.1 directory structure code 1 $ tree. | -- 2. log' -- backup '-- 1.log 3 directories, 0 files1.2 simple find command code 2 $ find-path. /backup. /backup1.3 incorrect wildcard use code 3 $ mkdir backup123 $ find-path. /backup * find: paths must precede expressionUsage: find [path...] [expression] The find command reports an error: the path must be expressed first. Problem Analysis (from: http://www.cnblogs.com/baibaluo/archive/2012/08/16/2642403.html): when there are multiple backups under the directory *, shell command to find-path backup backup123. at this time,-name followed by 2 matching characters, shell reported an error. Solution: the string matching the-path (-name) must be enclosed in single or double quotation marks. 1.4 correct wildcard writing code 4 $ find-path './backup *'./backup./backup/1. log./backup123 at this time, the command runs correctly! 2 find multi-condition expr1 expr2-o expr3 is equivalent to expr1-a expr2-o expr3. it is similar to or not in other languages. And it is a multi-condition test with a short circuit value of 2.1 find: there is no return result with code 5 $ find-path./backup-name '*. log' $. The statement indicates the file whose path is path and whose name ends with. log. Obviously, it does not exist. This statement is used to search for all files ending with. log in backup. It should be: find-path '. /backup * '-name '*. log '2. 2 find multi-condition attempt: Or code 6 $ find-path '. /backup * '-o-name '*. log '. /backup. /backup/1. log. /2. log. /backup123 to this place, the experience of-a and-o is clear at a glance. This command shows all files in backup * And all files ending with. log. 3-prune's Experience: install these shell commands first: Code 7 $ find-path '. /backup *'. /backup. /backup/1. log. /backup123 $ find-path '. /backup * '-prune. /backup. /backup1233.1 basic usage of prune-prune in man is like this. If-depth is not given, true; do not descend the current directory. if-depth is given, false; no effect. if the-depth option exists in the find statement,-prune will be ignored. Otherwise,-prune declares that the current path is not expanded. In this way, the backup path is not expanded due to the existence of the-prune option in the preceding commands. So 1. log is not in the print list. Let's try again: code 8 $ touch backup123/3.log$ find-path '. /backup * '-prune. /backup. /backup123 $ find-path '. /backup *'. /backup. /backup/1. log. /backup123./backup123/3. log prints the same result as expected. As described in the above 2 find multi-condition, find-path '. /backup * 'to get all the files with the backup prefix, and then compare the result with the-prune phase: in fact, it is to determine whether the former result contains the sub-files (folders) in the specified path ). 3.2 prune uses the exclusion path. In general, prune uses the Code 9 $ find-path '. /backup * '-prune-o-name '*. log'-print. /2. log indicates that all files except the backup * folder are removed from the current path *. log File. 3.2.1 this is an expected problem, but we can execute a command like this: Code 10 $ find-path '. /backup * '-o-name '*. log'-print. /2. log returns the same results. 3.2.2 further analyzing this problem, we will put it aside for the moment. Let's continue to look at the two Commands: Code 11 $ find-path '. /backup * '-prune-o-name '*. log '. /backup. /2. log. /backup123 $ find-path '. /backup * '-o-name '*. log '. /backup. /backup/1. log. /2. log. /backup1232 result sets are missing. /backup/1.log,-prune only implements the function of shrinking paths. Continue to compare the two commands with the above two commands. What is missing is a-print. in fact, there is such a sentence in man: "If no expression is given, the expression '-print' is used. "That is to say,-print is the default value, so the above two groups of commands can be viewed as follows: Enter the actual command find-path '. /backup * '-o-name '*. log 'Find \ (-path '. /backup * '-o-name '*. log' \)-printfind-path '. /backup * '-o-name '*. log'-print find \ (-path '. /backup * '\)-o \ (-name '*. the snippets in log '-print \) Code 9 and code 10 can be understood as print-path '. /backup * 'is false,-nam Find results where E' *. log' is true. The fragment in Code 11 prints all true results after filtering-path'./backup * '-o-name' *. log. 3.2.3 In conclusion, the exclusion path actually places-print after-o as the output. -Path './backup *' has been executed, and true is returned. The ticket is not printed. So is it true that-prune is useless? 4. Conclusion: from a more perspective, find can be considered as an inevitable full-text search for a specified path, by default, deep search is performed first (only when-depth is specified ). After finding the full-text search, you can throw the results to the following filtering conditions and filter the results one by one based on the rules of sum or non. An item whose return value is true is printed. In a java project, the total number of files is tens of thousands due to the large project. To find all the java files under the deepest level 2 directory. Find. -name "*. java "-maxdepth 2 find. -maxdepth 2-name "*. java "and so on, obviously the execution efficiency of the second command will be faster !!! -Maxdepth 2 performs a result filter to a great extent. When writing the find command, we should put the result that can minimize the result set to the front.

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.