7

I can see the -prune of find not working correctly. I guess -name "efence*" -prune option should select (or find) all files except the one with name efence* right?

Or am i wrong in my understanding?

The command i executed: find * -maxdepth 0 -name "efence*" -prune

Expectation: Select all files at current directory (maxdepth 0) except one with name *efence.

Please help me to understand -prune

RajSanpui
  • 11,556
  • 32
  • 79
  • 146

1 Answers1

10

Try

find * -maxdepth 0 -name "efence*" -prune -o -print

The prune option does print matching files, if no other options are specified (it still prevents find from recursing into matching directories, however).

Edited to add explanation:

find expressions distinguish between tests and actions. From man find:

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 performed on all files for which the expression is true. [my emphasis]

So -prune is an action which has the side effect that find will not recurse into subdirectories which match the preceding test (in your case, -maxdepth 0 -name "efence*"). But in terms of the truth-value of the expression, it's equivalent to just having

find * -maxdepth 0 -name "efence*" -true

and since you didn't specify any other action, -print is assumed (this assumption is always present as it allows you to type e.g. find . -name "*.java" instead of find . -name "*.java" -print).

Hope that makes sense. The accepted answer at the other thread talks about the same thing.

Community
  • 1
  • 1
OpenSauce
  • 8,533
  • 1
  • 24
  • 29
  • I am accepting your answer, but please explain me how does `-o print` does the magic. – RajSanpui Aug 01 '11 at 14:17
  • For the sake of completion, as a response to kingmasher1's question in comment. What OpenSauce explains is that `find * -maxdepth 0 -name "efence*"`, `find * -maxdepth 0 -name "efence*" -prune`, `find * -maxdepth 0 -name "efence*" -prune -print` yield the same as `find * -maxdepth 0 -name "efence* -true" where `-print` is implied and the test `-true` ... always evaluates to "true". I.e. with that instruction, you find all files whose names match the pattern `"efence*"`. Here there is no alternative action to pruning so `find ..... -prune` actually prints what it would normally prune. – Cbhihe Oct 03 '20 at 15:32
  • _[continued from above]_ The alternative to pruning comes as the `-o -print` action in `find * -maxdepth 0 -name "efence*" -prune -o -print`. In that case the section `find * (....) -prune` actually prunes results when the test(s) `(...)` evaluate(s) to "true" and prints it when it does not. You will find that the combination of `find * (...) - prune -o -print` is a common "conditional print pattern" (my terminology) when you use `find`. – Cbhihe Oct 03 '20 at 15:39