Find-prune parsing,

Source: Internet
Author: User

Find-prune parsing,

Find-prune resolution

Gl @ gl :~ $ Find./-name 'hello'

./Info/hello

./Hello

./Temp/hello

Gl @ gl :~ $ Find./-path './temp'-prune

./Temp

Gl @ gl :~ $ Find./-path './temp'-print

./Temp

Gl @ gl :~ $ Find./-path './temp'-prune-a-print

./Temp

Gl @ gl :~ $ Find./-path './temp'-prune-o-name 'Hello'-print

./Info/hello

./Hello

Gl @ gl :~ $ Find./-path './info'-prune-o-name 'Hello'-print

./Hello

./Temp/hello

Gl @ gl :~ $ Find./\ (-path './temp'-o-path'./info' \)-prune-o-name 'Hello'-print

./Hello

Note: The left and right sides of '\ (' and '\)' must be blank and there is no space between '\ (and.

If you want to find the tmp.txt file under the current directory (/home/student.pdf, but want to avoid the sep directory:

? Find/home/student-path/home/student/sep-prune-o-name "tmp.txt"-print

?

? You cannot add/That is,/home/student/sep/to the end of sep. If the current directory is/home/student, you can do the same.

? Find.-path./sep-prune-o-name "tmp.txt"-print

Conclusion:-path only matches the find path. You can use the matching symbol * []? For example:

?

? [Student @ bioeng ~] $ Find.-name file

./File

./Dir/file

./Dir/dir555/file

./Dir/dir2/file

./Dir/dir1/file

[Student @ bioeng ~] $

?

? [Student @ bioeng ~] $ Find.-path "* dir [12]"-prune-o-name file-print

./File

./Dir/file

./Dir/dir555/file

?

? [Student @ bioeng ~] $ [Student @ bioeng ~] $ Find.-path "* dir *"-prune-o-name file-print

./File

? [Student @ bioeng ~] $

Understanding of the find parameter-prune

-Prune is like a judgment sentence. When the expression math in front of-prune is found, a result of 1 is output after-prune is executed. If shell is used,

Echo $? If-prune is followed by the-o option, the C language syntax is 1 |-print, so we can see that

If the expression before-prune is true, the content after-o will not be executed. If it is not true, that is, 0 |-print, the output will be printed,

In addition, you must note that-path cannot be added to the end /,

For example, the path/vobs/gliethttp/signature cannot be written as/vobs/gliethttp/signature/. This is a hard rule.

Find/vobs/tmp/-path/vobs/tmp/signature-a-print

If you find., the relative path./gliethttp must be used for the backend-path.

Except for "find", all other searches, such as "find tmp" or "find/vobs", must use absolute paths.

? Let's take a look at what the/mnt directory has:

~ $ Ls-l/mnt

Total usage 0

-Rwxr-xr-x 1 root 0

-Rwxr-xr-x 1 root 0 2010-11-21 :34 a.txt

Drwxrwxrwx 1 root 0 2010-11-20 20: 22 shared

??? Run the following command:

?

~ $ Find/mnt-path "/mnt/shared"-prune-o-print

/Mnt

/Mnt/a.txt

/Mnt/

?

?

??? Run the following command:

?

~ $ Find/mnt-path "/mnt/shared"-prune

/Mnt/shared

?

?

~ $ Find/mnt-path "/mnt/shared"-prune-a-print

/Mnt/shared

??? Run the following command:

~ $ Find/mnt-path "/mnt/shared"-prune-print

/Mnt/shared

?

??? Why is the difference?

??? In fact, this command $ find/mnt-path "/mnt/shared"-prune-o-print should be divided into several sections for understanding.

??? Find/mnt-path "/mnt/shared" is the most basic find query. If the directory is shared, the result is true.

??? If the-prune option is added, the string is not used as the template style for searching for files or directories.

??? -Print option. If the return value of the find command is True, the file or directory name is listed to the standard output.

??? So find/mnt-path "/mnt/shared"-prune goes for the search. If dir1 is found, find returns true, and-prune does not actually work .?

??? Find/mnt-path "/mnt/shared"-prune-o-print, because-prune and print are in the or relationship, if find returns true, run prune first, it is ignored and no output. If find returns false and then runs prune, it is true and print.

For example, you need to find all files not in the dir1 subdirectory under the/usr/sam directory.

?

Find/usr/sam-path "/usr/sam/dir1"-prune-o-print

Find [-path...] [expression] After the path list is an expression

-Path "/usr/sam"-prune-o-print is-path "/usr/sam"-a-prune-o-print short expressions are evaluated in order, -both a and-o are short-circuit values. They are similar to shell's & | if-path "/usr/sam" is true, the values are-prune, -prune returns true, which is true to the logical expression. Otherwise, the Value-prune is not required, and the logical expression is false. If-path "/usr/sam"-a-prune is false, evaluate-print,-print to return true, or the logical expression is true; otherwise, do not request a value-print, or the logical expression is true.

The special expression combination can be written

?

If-path "/usr/sam" then

?????????? -Prune

Else

?????????? -Print

Avoid Multiple folders

?

Find/usr/sam \ (-path/usr/sam/dir1-o-path/usr/sam/file1 \)-prune-o-print

Note: parentheses () indicate the combination of expressions. That is, it indicates that shell does not give a special explanation for the subsequent characters, but leaves it to the find command to explain its meaning. Since the command line cannot directly use parentheses, you need to use the backslash '\' to compress (that is, the '\' to enable the command line to recognize the parentheses ). Note that spaces are required on both sides.

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.