Linux Command find (2), linux Command find

Source: Internet
Author: User

Linux Command find (2), linux Command find

The previous article has introduced the test of expressions, which is the core of the find command. Now we will introduce the options and actions of expressions.
(1) options

  • -D,-depth: the two serve the same purpose. man's manual and many blogs say that their role is to first find the current directory file and then find it in its subdirectory. However, I tried it many times and found that it was ineffective... Please advise me ~

  • -Maxdepth: specifies the maximum directory depth, that is, the number of subdirectories of the specified directory. 1 indicates that only the directory itself is specified; 2 indicates that the first-level subdirectory; 3 indicates the "Sun Directory", and so on...

 m@meng:~/patches/tmp$ find . -maxdepth 1 -name 'onlyme*'  ./onlyme ./onlyme1 m@meng:~/patches/tmp$ find . -maxdepth 2 -name 'onlyme*' find . -maxdepth 2 -name 'onlyme*'  ./test/onlyme5 ./test/onlyme.sh ./onlyme ./onlyme1
  • -Mindepth: similar to-maxdepth, but the minimum depth is specified. For example
    Set 2, that is, only search for subdirectories with a depth greater than 2, regardless of those directories smaller than 2 (smaller than 2 is the specified directory itself), as shown in the following example:
 m@meng:~/patches/tmp$ find . -mindepth 1 -name 'onlyme*'  ./test/onlyme5 ./test/onlyme.sh ./onlyme ./onlyme1 m@meng:~/patches/tmp$ find . -mindepth 2 -name 'onlyme*'  ./test/onlyme5 ./test/onlyme.sh
  • -Help: displays help information and exits.
  • -Mount or-xdev: If a sub-directory is a mount point of another file system, skip this directory.
  • -Daystart: Read the man Manual Statement first: Measure times (for-amin,-atime,-cmin,-ctime,-mmin, and-mtime) from the beginning of today rather than from24 hours ago. it seems that this option only affects the time-related test, and changes the time calculation method, that is, it does not start from the current time, But starts from every day. As mentioned above,-atime n does not represent the actual "one day", but 24 hours. However, after the "-daystart" option is added, it can actually represent "one day, because the timing starts from every day, let's look at the example below:

    M @ meng :~ /Patches/tmp $ date 00:13:47 CSTm @ meng :~ /Patches/tmp $ stat new recent visit: 00:10:15. 707908310 + 0800 recent changes: 00:10:15. 707908310 + 0800 recent changes: 00:10:15. 707908310 + 0800m @ meng :~ /Patches/tmp $ stat test recent visit: 23:42:25. 064527595 + 0800 recent changes: 23:57:45. 095385673 + 0800 recent changes: 23:57:45. 095385673 + 0800m @ meng :~ /Patches/tmp $ find-atime 0 ../test./newm @ meng :~ /Patches/tmp $ find-daystart-atime 0 ../new

    Obviously, before the "-daystart" option is added, "atime 0" indicates that if a file has been accessed within 24 hours, if-daystart is added,-atime 0 indicates that the files that have been accessed today will match. Similarly, atime 1 indicates yesterday (7/11 ~ 7/11) The accessed file will match.
    The function of this option can be simply summarized as: to reduce to an integer.

(2) actions

  • -Delete: delete a matched file. The man manual mentioned: Use of-delete automatically turns on the-depth option, but I still don't know what the-depth is ..
  • -Print: This is the default action, that is, when no action is specified, this-print is actually used, but when another action is specified, it must be explicitly added with-print for the effect. It prints each matching file to the standard output, with one file per row. This means that a line break is added at the end of each file during-print printing.
    However, this linefeed is used for browsing convenience and sometimes causes some trouble, such as when using pipelines. to omit this linefeed, you can use-print0 instead, as shown below:
 m@meng:~/patches/tmp$ find -daystart   -atime 0 -print . ./new m@meng:~/patches/tmp$ find -daystart   -atime 0 -print0 ../newm@meng:~/patches/tmp$

Therefore, if you only want to observe the output on the terminal, it is not necessary to use-print0 and-print.
-Another variant of "-printf" is "-printf", which is similar to the printf function in C language. It is not studied for the time being. There are too many formats.

  • -Ls: similar to the-dils Option combination of the ls command, as follows:
M @ meng :~ /Patches/tmp $ find-daystart-atime 0-ls 7478778 4 drwxrwxr-x 3 m 4096 July 12 00:10. 7478967 0-rw-r -- 1 m 0 July 12 00:10. /new m @ meng :~ /Patches/tmp $ ls-dils new 7478967 0-rw-r -- 1 m 0 July 12 00:10 new

For a more detailed output.

  • The f series, namely-(fls/fprint/fprint0/fprintf) file. These actions are very similar to those without the start of f, instead of printing the results to the standard output, the results are output to the file.

  • -Exec command; and-exec command {} +
    -Exec itself only executes another command after the find statement is executed. I thought-exec is similar to the pipeline. The file found by find will be directly processed by command, but the result is not like this; to achieve the effect of the pipeline, add "{}" after the command, which represents the file found above.
    Then there is a syntax problem: If "{}" is not followed by a command, it must end with a space and a semicolon; if "{}" is followed by a command, you can end with the previous one, you can also use a space and the end of a "+;
    To prevent shell extensions, semicolons must be escaped. You must either add a backslash to the front or enclose it in quotation marks. Similarly, "{}" should also be protected by quotation marks.

M @ meng :~ /Patches/tmp $ find.-name new-print-exec cat onlyme1 \;./new haha m @ meng :~ /Patches/tmp $ find.-name new-print-exec cat onlyme1 + find: the "-exec" Parameter m @ meng: ~ is missing :~ /Patches/tmp $ find.-name new-print-exec cat {}\;./new hello m @ meng :~ /Patches/tmp $ find.-name new-print-exec cat {} +./new hello m @ meng :~ /Patches/tmp $ find.-name new-print-exec cat {} ';'./new hello m @ meng :~ /Patches/tmp $ find.-name new-print-exec cat {} ";"./new hello

The first example shows that the command and find can be completely independent. The second example shows that the command can only end with a semicolon; the following examples describe the end.

  • -Execdir command; and-execdir command {} +
    Similar to the previous option, the command after-exec is executed in the current directory, while-execdir is executed in the subdirectory where the matching file is located. Run the pwd command to test:
m@meng:~/patches/tmp$ lsmagic.mgc  new  onlyme  onlyme1  testm@meng:~/patches/tmp$ ls test/onlyme5  onlyme.shm@meng:~/patches/tmp$ find . -name onlyme.sh -exec pwd \;/home/m/patches/tmpm@meng:~/patches/tmp$ find . -name onlyme.sh -execdir pwd \;/home/m/patches/tmp/test

The onlyme. sh file is in the subdirectory test. find and execute the pwd command after finding it. However, the results of-exec and-execdir are different.

  • -OK and-okdir
    It is basically the same as-exec and-execdir, but you will be asked before execution.
  • -Prune
    If the file is a directory, do not descend into it. That is to say, if a directory matches, the directory will not be searched.
m@meng:~/patches/tmp$ find . -name 'test*' ./test./test/test1m@meng:~/patches/tmp$ find . -name 'test*' -prune ./test

We can see that the matching-name 'test * 'includes the test sub-directory itself and the test1 file under the test directory. However, after the-prune option is added, it will not be searched in the test directory, because the test directory is also
Matching items. -Prune often works with-path to exclude certain subdirectories.

  • -Quit
    Exit immediately, and the command after-quit will not be executed. As follows:
m@meng:~/patches/tmp$ find . -name new -print -exec cat {} ";"./newhellom@meng:~/patches/tmp$ find . -name new -print -quit -exec cat {} ";"./newm@meng:~/patches/tmp$ find . -name new -print -exec cat {} ";" -quit./newhellom@meng:~/patches/tmp$ find . -name new -quit -print -exec cat {} ";" m@meng:~/patches/tmp$

(3) logical operations
First, let's throw a piece of man's manual information:
The expression is made up of options (which affect overall operation rather than the processing of a specific file, and always return true), tests
(Which return a true or false value), and actions (which have side effects and return a true or false value), all separated by operators.
-And is assumed where the operator is omitted.
If the expression contains no actions other than-prune,-print is saved med on all files for which the expression is true.
That is to say, the three components of expression are actually boolean expressions, and expressions all have values, either true or false. In the manual, the options value is always true, while the other two values can be true or false.
In this way, there will be logical operations, in fact, mainly between the test operations, because an expression can have multiple test, the test can be between and, Or, non-calculation.
For example, in the previous article:

m@meng:~/patches$ find . -path ./tmp -o -name onlyme./tmp./tmp/onlyme

Now, the basic usage of the find command is complete ~ Tired. The above summary is definitely not perfect. I will try to fix it later.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.