[SOLVED] find command with use of -not and -or ; why the order matters
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
find command with use of -not and -or ; why the order matters
I am trying to understand why the results differ if I use -not -path to exclude the "Exclude" directory from the search before and after the -iname conditions.
For referece , all files with *.bmp, *.png and *.jpg file extension:
I wanted to understand why there is difference - first I found 28128 files with -not being upfront and then I get 28715 if the -not is kept in the end?
Between every pair of options that does not have an -or has an implied -and whether you write it or not. The precedence of -or and -and is quite different. Remember your boolean logic.
If you read the man pages for find, you actually see:
Code:
expr1 -o expr2
Or; expr2 is not evaluated if expr1 is true.
expr1 -or expr2
Same as expr1 -o expr2, but not POSIX compliant.
So, I experimented a bit and the command acts literally how it is explained above. Since you have several "-or", the first expression is being evaluated for sure. Some of the subsequent ones are being ignored if the previous expression is true. If you test this command with a less populated directory where you can actually examine the output without wc, you will see that in the case where the exclusion comes at the end, the command actually does not exclude the path since one or more of the previous "or" expressions were true.
Last edited by aragorn2101; 06-07-2017 at 02:50 AM.
I wanted to understand why there is difference - first I found 28128 files with -not being upfront and then I get 28715 if the -not is kept in the end?
The "-and" operator (whether written or implied) has higher precedence than "-or", so in the first expression, the "-type f" and "*Exclude*" terms bind only to the "*.bmp" term. It is equivalent to this:
@pan64 since this happened with some large database of files containing thousands of files, I found it difficult to learn it by looking through the file list. Creating a separate trial db was more work without clarity at that point to ensure that it will reproduce the same behavior.
Thank you very much for the explaining the root cause Turbocapitalist, Aragorn2101, and rknichols.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.