find Command Logical AND, OR and NOT Examples
The Linux find command is a very versatile and powerful command and is frequently used while working on the command line. it is used to search for files and directories that match a particular criterion or more than one criteria. Logical AND, OR and NOT Examples.
We could search for files by permissions, users, file type, date, size, creation/modification time and other possible requirements. By using the – exec options with the find command, other UNIX commands can be executed on resulting matches found by the find command.
In this article, however, we would not be presenting the full feature set that find has to offer but instead, focus on an interesting subset of its features. We will show you how we could perform logical AND, OR and NOT operations on our search criteria while working with the find command. So, for this article, we assume that you have a basic understanding of how the find command works. If not refer this previous article to learn
Find Command – Logical AND
Included this mainly for the sake of completeness actually. This is because every search we perform using the find command mentioning more than one search criteria is an implicit logical AND operation.
Consider the below example
$ find . -name "*.bash" -mtime +180 -size +2K -exec ls -l {} \;
In the above command, we are telling find to search for files/directories with the string .bash in their name, they should be older than 180 days and should have a size greater than 2KB.
Finally, we execute the ls -l command on the results produced from the find command by using the -exec option.
So find would search for and display only those files matching all the three conditions thereby performing a logical AND operation.
Logical OR Along with Find Command
Let’s consider a scenario wherein we need to modify the example we used previously and obtain files with the strings .bash as well as .txt. To fulfill this requirement use the -o option with the find command to indicate a logical OR operation.
Given is the complete command
$ find . \( -name "*.bash" -o -name "*.txt" \) -mtime +180 -size +2k -exec ls -lh {} \;
In the above example we’ve used -o to perform a logical OR operation for the file extensions but the other conditions are applied as an implicit logical AND operation.
It is recommended that you enclose the file extensions in a bracket, and also use the \ ( backslash) escape character as in the command.
I’ve tried the command it without using the braces and it did not work.
Could even have a combination of multiple OR conditions like we see in the following example:
$ find . \( -name "*.bash" -o -name "*.txt" \) \( -mtime +180 -o -mtime -10 \) -size +2K -exec ls -ltr {} \;
Here I’ve just modified the previous example slightly to add an OR condition with our -mtime (modified time) criteria to search for files modified more than 180 days ago or less than 10 days ago.
Logical NOT Examples
We’ve seen logical AND and logical OR operations. Now we’ll take a look at logical NOT i.e we’ll perform a negation search.
To negate a search criterion we proceeded the search criteria with an exclamation (!) symbol.
Example to find list all files that were modified in the last 30 days and exlude .txt extension
$ find . -type f ! -name "*.txt" -mtime -30 -exec ls -l {} \;
Conclusion
This article, we showed you some examples where you could use the capabilities provided by find and perform some interesting and highly customized search operations.
We hope you found this article useful and look forward towards your feedback.
Related Articles
Implementing Quota management on File System
Download any video from any site using this tool
Thanks for your wonderful Support and Encouragement