2

From https://unix.stackexchange.com/a/81379/674

  1. -path does not (re)define the start path. It refers to the combination of the start path and the relative path of the currently examined object.

    • Does "start path" mean the argument path to find?:

      find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
      
    • What does "combination of the start path and the relative path of the currently examined object" mean?

  2. You may by this e.g. find all files within a subdirectory src no matter on which level.

    How is 'find all files within a subdirectory src no matter on which level" done?

  3. "do not treat / or . specially" means that * can match both file names including the extension and into subdirectories: [...]/file* would match both /file.txt and /files/foo.bar

    "do not treat / or . specially" doesn't mention *, so does "do not treat / or . specially" only apply to *?

Tim
  • 101,790

2 Answers2

5
  1. Yes, "start path" means one of the directory names that appear near the beginning of the find command, after the options but before the expression.  I interpret the question that you linked to as suggesting that that OP was confused about the difference between

    find /etc ...
    

    and

    find -path /etc ...
    

    • "It refers to the combination of the start path and the relative path of the currently examined object."

      I guess the author of that answer is imagining that you have a directory called tools/crowbar, which contains files drink, food, foolishness, and wisdom.  If you say

      find tools/crowbar -path "*bar/foo*"
      

      it will find tools/crowbar/food and tools/crowbar/foolishness, but not the other two.

  2. I agree with lcd047.  To put it another way, if you have a directory structure like

    .
    ├───cat
    │   ├───dog
    │   │   └───kennel
    │   └───tac
    │       └───src
    ├───dest
    ├───original
    │   ├───recipe
    │   └───src
    └───src

    then

    find . -path "*/src/*"
    

    will find things in all three src folders (and all subdirectories thereof) without reporting things in cat, cat/dog, cat/dog/kennel, cat/tac, dest, original, or original/recipe.

  3. It helps to read entire paragraphs, and not just cherry-pick sentence fragments and expect them to make sense in isolation.  The discussion of the -path test in find(1) says,

    -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).  ...

      OK, when you see "file name", "shell pattern", and "metacharacters" all on the same line, you’re expected to think of the shell’s pattern matching / pathname expansion, which has special pattern characters *, ?, and [...].  ("Metacharacter" is basically a $10 word for "special character".)  And then it goes and shows you an example with a * in it!  So you should be able to figure out what it’s talking about.

      So, what is it saying?  Look at the example: sr*sc matches src/misc. This is a difference from pathname expansion in the shell, where you would generally need to use something like sr*/*sc to match src/misc.  And, no, this doesn’t apply only to *; -path "sr????sc" and -path "sr[cim/][cim/][cim/][cim/]sc" work the same way.

      And what they don’t bother to mention is that -path "*sr*sc" will match not only src/misc but also .src/misc and src/.misc; even though *sr*/*sc generally won’t match those (in the shell) because, in the shell’s pathname expansion, * generally doesn’t match names that begin with ..

    3

    To avoid confusion you should probably think of the prototype for find(1) like this:

    find [-H] [-L] [-P] [-D debugopts] [-Olevel] [dir...] [expression]
    

    find(1) finds files in dir. expression is a logical expression formed of tests that are applied to the files found. -path, -name, -type are examples of such tests.

    1. above basically says -path doesn't set dir, but it's argument is taken as relative to dir.

    2. says -path '*src*' matches both ./src/foo/bar/test.c and ./foo/src/bar/test.c, but not ./foo/bar/test.c.

    3. says * is a wildcard, and it matches across directories. It also says you shouldn't confuse it with * from the DOS / Windows world, where you need to write *.* to match files with "extensions".

    Or at least this is my understanding of it.

    lcd047
    • 7,238