Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Am I doing something wrong?

>Solution :

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).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading