Home > Net >  Why do I need '-o' in linux find command?
Why do I need '-o' in linux find command?

Time:11-10

I want to list files with certain name pattern under certain directory, and excluding certain sub-directory. By doing

find "../../" -path "../../backup" -prune -regex "\.*\.v" -print

nothing is outputted. But by adding -o

find "../../" -path "../../backup" -prune -o -regex "\.*\.v" -print

I get the correct results. -o means or. But I don't think there is an or logic in my requirements, I think it should be and?

file name with certain pattern & under certain directory & not under certain sub-directory

Am I doing something wrong?

CodePudding user response:

From the find man page:

 -prune True;  if the file is a directory, do not descend into it.  
If -depth is given, false; no effect.  

  expr1 -o expr2
   Or; expr2 is not evaluated if expr1 is true.

The construct -prune -o \( ... -print0 \) is quite common.  
The idea here is that the  expression before -prune matches 
things which are to be pruned.
However, the -prune action itself returns true, so the following 
-o ensures that the right hand side is evaluated only for 
those directories which didn't get pruned (the contents of 
the pruned directories are not even visited, so their contents are irrelevant).
  • Related