4

According to the manpage of GNU find, -print action is used by default and

Actions which inhibit the default -print are -delete, -exec, -execdir, -ok, -okdir, -fls, -fprint, -fprintf, -ls, -print and -printf.

So -prune action should still imply -print action.

Actually, it does.

$ tree .
.
├── dir/
│   └── file2
└── file1

$ find . -name dir #0
./dir

$ find . -name dir -prune #1
./dir #printed as expected

$ find . -name dir -prune -or -name file1 #2
./file1
./dir #printed as expected

However, sometimes -prune inhibits the default -print.

$ find . -name dir -prune -or -name file1 -print #3 #last -print is only added to the above example
./file1

$ find . -name dir -prune -or -print #4
.
./file1

How can I understand this contradiction?


My Understanding:

#1

  1. file1 doesn't satify -name dir so skipped.

  2. dir satisfies -name dir so pruned and dir is added to TODO list.

  3. -print is additionally applied to dir in TODO list.

#2

  1. file1 satisfies -name file1 so added to TODO list.

  2. same as #1-2

  3. -print is additionally applied to dir and file1 in TODO list.

#3

  1. same as #2-1

  2. same as #2-2

  3. -print is applied to file1 in TODO list.

  4. -print should additionally be applied to dir because -prune doesn't inhibit -print. (But this is incorrect. WHY?)

#4

  1. file1 is added to TODO list.

  2. same as #3-2

  3. same as #3-3

  4. same as #3-4

(Actually there is no TODO list in find. See this comment and the standard.)


Supplement:

As pointed out in oguz ismail's answer (deleted now), my question is not related to -prune. However, the question is not solved.

Let us think about -name A -o -name B -print. This is broken into two expressions: -name A or -name B -print.

My understanding: The first expression -name A doesn't have an action. So -print should be implied. In other words, -name A -o -name B -print should be interpreted as -name A -print -o -name B -print.

Actual behavior: -name A -o -name B -print is one compound expression. There is -print in this compound expression. So no additional -print should be implied.

There is ambiguity but I believe my interpretation is more natural because, in this case, only -name A or -name B -print is satisfied by each file (both expressions are never satisfied at the same time)

John Kugelman
  • 307,513
  • 65
  • 473
  • 519
ynn
  • 1,867
  • 10
  • 22
  • You might want to look at [How to use '-prune' option of 'find' in sh?](https://stackoverflow.com/q/1489277/5291015) and [What does -prune option in find do?](https://stackoverflow.com/q/6896564/5291015) – Inian Oct 24 '19 at 13:33
  • There is no such thing as TODO list in find, if an entry makes it to -print, it's printed; that's all. The expression is evaluated separately for every single entry – oguz ismail Oct 24 '19 at 14:11
  • @oguzismail I don't think whether TODO list really exists is essential. – ynn Oct 24 '19 at 14:47
  • 1
    Wrt your last edit, POSIX mandates such behavior; so this is not a question but a suggestion to standard maintainers. At first it was a question, and it's answered – oguz ismail Oct 24 '19 at 14:49
  • 1
    It is, misuse of terminology leads to misapprehensions. Like, the word *action* is never used in POSIX find specification, but GNU's misuse of that word led you to this confusion – oguz ismail Oct 24 '19 at 14:50
  • 1
    https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html Read the document as a whole – oguz ismail Oct 24 '19 at 14:59

1 Answers1

2

As written in this comment and this comment, my question, which is summarized in the Supplement section in OP, has come from the ambiguity in manpage of GNU find and POSIX has a better explanation. I found this is true.

POSIX says

(If no expression is present, -print shall be used as the expression. Otherwise,) if the given expression does not contain any of the primaries -exec, -ok, or -print, the given expression shall be effectively replaced by:

( given_expression ) -print

and it is natural to interpret given_expression is a compound expression which consists of one or more sub-expressions because it is closed in parenthesis. (If this given_expression referred to a single sub-expression, the parenthesis would definitely be redundant.)

Community
  • 1
  • 1
ynn
  • 1,867
  • 10
  • 22