Linux command Find (ii)

Source: Internet
Author: User

The previous article has introduced expressions's test, which is the most important thing in the Find command, and now introduces the options and actions of expressions.
(i) Options

    • -d,-depth: The two roles are the same, and the man manual and many blogs say their role is to find the current directory file first, and then find it in its subdirectories. But I have experimented with it many times and found no effect ... Please the Expert

    • -maxdepth: Specifies the maximum directory depth, which is the level of subdirectories of the specified directory: 1 represents just the specified directory itself, 2 represents a primary subdirectory, 3 is the "Sun directory", and so on ...

 m@meng:~/patches/tmp$ 1‘onlyme*‘  ./onlyme ./onlyme1 m@meng:~/patches/tmp$ 2‘onlyme*‘2‘onlyme*‘  ./test/onlyme5 ./test/onlyme.sh ./onlyme ./onlyme1
    • -mindepth: Similar to the above-maxdepth, but specifies the minimum depth. For example, referring
      2, that is, only the search depth of more than 2 levels of subdirectories, regardless of less than 2 of those directories (less than 2 is the specified directory itself), the following example:
 m@meng:~/patches/tmp$ 1‘onlyme*‘  ./test/onlyme5 ./test/onlyme.sh ./onlyme ./onlyme1 m@meng:~/patches/tmp$ 2‘onlyme*‘  ./test/onlyme5 ./test/onlyme.sh
  • -help: Displays the help information and exits.
  • -mount or-xdev: If a (sub) directory is a mount point for another file system, the directory is skipped.
  • -daystart: First Look at the Man Handbook: Measure times (For-amin,-atime,-cmin,-ctime,-mmin, And-mtime) from the beginning of today Rathe R than From24 hours ago. It seems that this option only affects the time-related test, and changes the way the time is calculated, not counting from the current moment, but starting at 0:00 every day. As previously said,-atime N, this n does not represent the actual "day", but refers to 24 hours, but with-daystart this option, it can really represent the "day", because this is the day from 0:00 to start time, see the following example:

    M@meng: ~/patches/tmp$ Date -Years -Month ADay and Sundayxx: -: - CSTM@meng: ~/patches/tmp$ Stat New recently visited: -- -- A xx:Ten:15.707908310+0800Recent Changes: -- -- A xx:Ten:15.707908310+0800Recent Changes: -- -- A xx:Ten:15.707908310+0800M@meng: ~/patches/tmp$ Stat Test recently accessed: -- -- One  at: the:25.064527595+0800Recent Changes: -- -- .  at: $:45.095385673+0800Recent Changes: -- -- .  at: $:45.095385673+0800M@meng: ~/patches/tmp$ Find-atime0.. /test./newm@meng: ~/patches/tmp$ Find-daystart-atime0.. /new

    This is obvious, before adding-daystart this option,-atime 0 refers to the current moment, the forward 24 hours, if a file has been accessed, then match, but with-daystart,-atime 0 means that the files accessed today will match. Similarly-atime 1 means that files that were accessed yesterday (that is, 7/11 0:00 ~ 7/11 23:59) are matched.
    The function of this option can be simply summed up as: peers.

(ii) Actions

    • -delete: After the matching file is found, it is deleted. The man manual mentions: Use of-delete automatically turns on the-depth option, but I still don't know what the hell this-depth is for.
    • -print: This is the default action, that is, when you do not specify any action, you actually use the-print, but when you specify another action, you must explicitly add-print to make the effect. It prints each matching file to the standard output, one file per line. This means that-print adds a newline character at the end of each file when printing.
      But this line break is for easy browsing, sometimes causing some trouble, such as when using the pipe, if you want to omit this line break, you can use-PRINT0 instead, as follows:
 m@meng:~/patches/tmp$ find -daystart   0 -print . ./new m@meng:~/patches/tmp$ find -daystart   0 -print0 ../newm@meng:~/patches/tmp$

Therefore, if you just want to see the output at the terminal, it is not necessary to use-print0,-print.
-print also has a variant of-printf, similar to the printf function in C, to format the output, temporarily not researched, there are too many formats.

    • -ls: A combination of-dils options similar to the LS command, as follows:
M@meng: ~/patches/tmp$ Find-daystart-atime0-ls7478778    4Drwxrwxr-x3Mm4096  7Month A xx:Ten.7478967    0-rw-rw-r--1Mm0  7Month A xx:Ten./new m@meng: ~/patches/tmp$ Ls-dils New7478967 0-rw-rw-r--1Mm0  7Month A xx:TenNew

A more detailed output is changed.

    • F-Series, i.e.-(fls/fprint/fprint0/fprintf) file, these actions are very similar to those that do not have the start F, but instead of printing the results to the standard output, they are output to file files.

    • -exec command; with-exec command {} +
      -exec itself in fact just after find executes another command, I thought-exec similar to pipeline, find the file will be directly command processing, the result is not so, if you want to achieve the effect of the pipeline, you need to add "{}" after the command, It represents the file found earlier.
      Then there is the grammatical problem: when there is no "{}" behind the command, you must end with a space and a semicolon, and you can use the preceding ending or a space and a "+" at the end of the command after "{}".
      To prevent the shell itself from extending, semicolons need to be escaped, either preceded by a backslash, or enclosed in quotation marks, and the "{}" is best protected with 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:Missing "-exec" parameter 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 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, and the second command can only end with a semicolon when there is no "{}" behind it, and the following examples illustrate the end of the "{}".

    • -execdir command; and-execdir command {} +
      Similar to the previous pair of options, except that the command behind-exec is executed in the current directory, and-execdir is executed in the subdirectory where the matching file resides. Let's test it with the PWD command:
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

File onlyme.sh in the subdirectory test, find executes the PWD command after it finds it, but the results of-exec and-execdir are different.

    • -ok and-okdir
      is basically the same as-exec and-execdir, but will ask the user first before executing.
    • -prune
      If the file is a directory, does not descend into it. That is, if a directory matches, then it will no longer enter this directory to search.
m@meng:~/patches/tmp$ ‘test*‘ ./test./test/test1m@meng:~/patches/tmp$ ‘test*‘ -prune ./test

We see that the matching-name ' test* ' includes the test subdirectory itself and the Test1 file in the test directory, but with the-prune option, it does not go into the test directory, because the test directory itself is a
A match. -prune often cooperate with-path to exclude certain subdirectories.

    • -quit
      Exit immediately, and the command after-quit will no longer execute. 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 /span>:~/patches/tmp  $  find. -name new-quit-print-exec Cat {}  m @meng  Span class= "Hljs-symbol" >:~/patches/tmp  $  

(iii) Logical operation
Let's throw a man's manual. Information:
The expression is made up of options (which affect overall operation rather than the processing of a specific file, and Al Ways return True), tests
(Which return a true or False value), and actions (which has side effects and return a true or False value), all separate D by operators.
-and is assumed where the operator is omitted.
If The expression contains no actions other Than-prune,-print are performed on all files for which the expression is true .
That is, the three components of expression are actually Boolean expressions, and the expressions are both valued, not true or false. The manual says that the value of the options is always true, while the other two values can be true false.
In this way, there will be logical operations, in fact, the arithmetic between the test, because an expression can have more than one test, these test can be carried out with, or, non-operation.
As in the previous article, for example:

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

At this point, the basic usage of the Find command is complete ~ very tired. The above summary certainly has the imperfect place, I will later slowly add slowly corrects.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux command Find (ii)

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.