Skip to main content

Logical Operators in the Find Command in Linux

The find command in Linux is a powerful tool used for searching files and directories in a directory hierarchy based on various criteria such as name, type, size, modification date, and permissions. An integral feature of the find command is its ability to combine different types of tests and actions using logical operators. These operators allow users to create more complex and precise search criteria.

Below is a table listing the logical operators available in the find command, along with a description of what each operator does:

-andEnsures that the tests on either side of the -and must both be true for the file to be included. This is the default operator if no other is specified.
-orIncludes a file if either the test before or after the -or is true.
!This is the NOT operator. It inverts the result of the test that follows it.
,This operator allows you to perform multiple independent tests/actions on the same set of files.
-notThis is the same as the ! operator; it inverts the result of the test that follows it. (GNU extension)
( )Parentheses are used to group tests and actions. You usually need to escape them to avoid shell interpretation.

Examples of Using Operators with the Find Command

Here are some examples of how these operators can be used to find files on an Ubuntu Linux system:

Example 1: Combining tests with -and

find /var/log -type f -name "*.log" -and -size +1M

This command searches for files in /var/log that have a .log extension and are larger than 1MB. The -and operator ensures both conditions must be true for a file to be matched. Since -and is the default, you could also omit it, and the command would function the same.

Example 2: Using -or to find files by different criteria

find /etc -name "apache2.conf" -or -name "nginx.conf"

This command will find files in /etc that are named either apache2.conf or nginx.conf.

Example 3: Inverting a test with !

find /home -name "*.txt" ! -user $(whoami)

This will find all .txt files in the /home directory that are not owned by the current user.

Example 4: Executing multiple independent tests/actions with ,

find / -name "php.ini" -exec echo "Found PHP config:" {} \; , -name "httpd.conf" -exec echo "Found HTTPD config:" {} \;

This command searches the entire filesystem for php.ini and httpd.conf. When either is found, it prints a message indicating which config file was found. Actions after each comma are treated independently.

Example 5: Grouping tests with parentheses

find / -type f \( -name "*.jpg" -or -name "*.png" \) -size +2M

This command searches for files across the entire system that are either .jpg or .png files and are larger than 2MB. Parentheses group the -name tests together and separate them from the -size test.

When using the find command with these operators, it is important to consider the order of operations and to use quotes or backslashes as necessary to ensure that the shell interprets the command correctly. The find command reads the criteria from left to right and applies them as soon as possible unless grouped with parentheses.

Always test complex find commands with non-destructive actions like -print before proceeding with actions that can change or remove files to avoid unintended consequences.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle