Skip to main content

Understanding Tests in the Find Command

The find command in Linux is a powerful tool for searching through the filesystem. One of its most potent features is the ability to use "tests" as part of the command to specify which files should be matched. These tests can check a variety of file attributes, such as their type, size, name, modification time, and more.

Tests are expressions that evaluate to true or false for each file find examines. When you combine these tests with logical operators, you can create complex queries to find exactly the files you're looking for.

Operators for Different File Types

Let's start with a table listing various operators that can be used to test different file types:

OperatorDescription
-fRegular file
-dDirectory
-lSymbolic link
-bBlock special file
-cCharacter special file
-pNamed pipe (FIFO)
-SSocket
-gSet-group-ID
-uSet-user-ID
-kSticky bit

Examples of File Type Tests

Find All Directories

find / -type d

This command will search the entire root directory for directories.

Find All Regular Files

find /home/user -type f

This will find all regular files under the /home/user directory.

find /var/log -type l

This will find all symbolic links within the /var/log directory.

Operators for File Sizes

Next, we have a table for operators related to file sizes:

OperatorDescription
-sizeFile uses specified size

You can specify the size in different units:

  • c for bytes
  • k for Kilobytes (KB)
  • M for Megabytes (MB)
  • G for Gigabytes (GB)

To find files of exactly a size, you just use the number followed by the unit. To find files smaller than a size, you use -size -number[unit]. For files larger than a size, you use -size +number[unit].

Examples of File Size Tests

Find Files Exactly 100KB in Size

find /home/user -type f -size 100k

This finds files in /home/user that are exactly 100KB.

Find Files Larger Than 1MB

find / -type f -size +1M

This will find all files larger than 1MB in the root directory.

Find Files Smaller Than 500 Bytes

find /etc -type f -size -500c

This searches for files in /etc that are smaller than 500 bytes.

Combining File Type and File Size Tests

You can also combine these tests to find, for example, regular files of a certain size:

find /var -type f -size +10M

This finds regular files under /var that are larger than 10MB.

Using Logical Operators with Tests

find also supports logical operators such as -and, -or, and -not (which can also be represented as ! for negation). These can be used to combine multiple tests:

Find Files That Are Either 100KB or 200KB in Size

find /home/user \( -size 100k -or -size 200k \)

This command will find files that are either 100KB or 200KB in size within /home/user.

Find Files Larger Than 10MB That Are Not Executable

find /usr -type f -size +10M -not -executable

This command searches for files larger than 10MB that are not executable within /usr.

Conclusion

Tests are fundamental to the utility of the find command, allowing users to search the filesystem with precision and efficiency. By understanding and utilizing these tests, you can tailor your file searches to exactly what you need, whether you're a system administrator, developer, or an end user trying to organize your files. With the find command's flexibility, you can filter files by type, size, permissions, and many other criteria to manage and process your filesystem effectively.

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