zoukankan      html  css  js  c++  java
  • Bash Command 1: find

    GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known, at which point find moves on to the next file name. If no starting-point is specified, . is assumed. (from man find)

    可以看出 find 命令的主要组成部分是 expression。

    The part of the command line after the list of starting points is the expression. This is a kind of query specification describing how we match files and what we do with files that were matched. An expression is composed of a sequence of things:

    • Tests
      Tests return a true or false value, usually on the basis of some property of a file we are considering. The -empty test, for example, is true only when the current file is empty.

    • Actions
      Actions have side effects (such as printing something on the standard output) and return either true or false, usually based on whether or not they are successful. The -print action for example prints the name of the current file on the standard output.

    • Global options
      Global options affect the operation of tests and actions specified on any part of the command line. Global options always return true. The -depth option for example makes find traverse the file system in a depth-first order.

    • Positional options
      Positional options affect only tests or actions which follow them. Positional options always return true. The -regextype option for example is positional, specifying the regular expression dialect for regular expressions occurring later on the command line.

    • Operators
      Operators join together the other items within items within the expression. They include for example -o (meaning logical OR) and -a (meaning logical AND). Where an operator is missing, -a is assumed.

    If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.

    The -delete action also acts like an option (since it implies -depth).

    我写这篇博客,最主要是为了指出 Tests 中的 -path 选项,我发现 -path 的参数必须是绝对路径。比如,当前路径下有一个文件
    problems/migrations/a.py
    find . -path "problems/migrations/*.py"
    并不能找到这个文件,得写成
    find . -path "./problems/migrations/*.py"

    manual page 中对 -path 选项的说明如下:

    -path pattern
    File name matches shell pattern pattern. The metacharacters do not treat / or . specially; so for example,
    find . -path "./sr*sc"
    will print an entry for a directory called ./src/misc (if one exists). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to skip the directory src/emacs and all files and directories under it, and print the names of the other files found, do something like this:
    find . -path ./src/emacs -prune -o -print

    Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path name here if the relevant start point is also an absolute path. This means that this command will never match anything:
    find bar -path /foo/bar/myfile -print

    find compares the -path argument with the concatenation of a directory name and the base name of the file it's examining. Since the concatenation will never end with a slash, -path arguments ending in a slash will match nothing (except perhaps a start point specified on the command line). The predicate -path is also supported by HP-UX find and will be in a forthcoming version of the POSIX standard.

    第三段中提到的 "the base name of the file" 指的是一个文件的「名字」,不包括该文件的路径。比如,文件 ./problems/migrations/a.py 的 base name 为 a.py。通过 basename 命令即可获得一个文件的 base name。

    第二段中的

    「the pattern match test」applies to 「the whole file name」starting from one of the start points named on the command line.

    大概能解释我遇到的问题:
    前面指定了一个 start point .(应视作一个 directory 而非字符 '.'?) 而后面的 pattern 中却不包含 . 这一 directory(即 current directory),所以匹配失败。

    这个问题涉及两个关键概念:「the base name of a file」 和 「the whole file name」。
    find 命令的工作机理有待深入探究。

  • 相关阅读:
    为什么new的普通数组用delete 和 delete[]都能正确释放
    虚幻4属性系统(反射)
    CFileDialog类的默认路径
    把单一元素的数组放在一个struct的尾端
    在UE4中使用SVN作为source control工具
    单精度浮点数和有效位数为什么是7位
    Valid Number--LeetCode
    归并排序
    堆排序
    直接选择排序
  • 原文地址:https://www.cnblogs.com/Patt/p/7201047.html
Copyright © 2011-2022 走看看