How to combine search criteria?

You can combine search criteria in the find command using logical operators. Here are the main operators you can use:

  1. AND (-a or simply by placing conditions next to each other): This operator allows you to specify that all conditions must be true for a file to be included in the results.

    find . -type f -name '*.txt' -size +1M

    This command finds all regular files (-type f) with a .txt extension that are larger than 1MB.

  2. OR (-o): This operator allows you to specify that at least one of the conditions must be true.

    find . -type f -name '*.jpg' -o -name '*.png'

    This command finds all files that are either .jpg or .png.

  3. NOT (-not or !): This operator allows you to exclude files that meet a certain condition.

    find . -type f -not -name '*.log'

    This command finds all regular files that do not have a .log extension.

Example of Combining Criteria

You can combine these operators to create more complex queries. For example:

find . -type f \( -name '*.txt' -o -name '*.md' \) -a -size +1M

This command finds all regular files that are either .txt or .md and are larger than 1MB. The parentheses \( and \) are used to group the conditions.

Note

When using logical operators, it's often a good practice to use parentheses to clarify the order of operations, especially when combining multiple conditions.

0 Comments

no data
Be the first to share your comment!