Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Linux find command

June 20, 2024

Using the Linux ‘find’ Command: A Comprehensive Guide with Examples

As a programmer, effectively managing files and directories is essential, especially when dealing with large codebases or projects. The Linux find command is a powerful tool that allows you to search for files and directories based on various criteria. In this blog post, we’ll explore the numerous options and provide extensive examples to help you master the find command.

Basic Syntax

The basic syntax of the find command is as follows:

1
find <path> <expression> <action>

where:

Searching for Files

Searching by Name

To find files by name, you can use the -name or -iname options. The former performs a case-sensitive search, while the latter is case-insensitive. Let’s look at some examples:

1
find /path/to/directory -name "*.txt"

This command will search for files with the extension .txt in the /path/to/directory and its subdirectories.

1
find /path/to/directory -iname "important.doc"

This command will search for a file named important.doc, ignoring the case, in the specified directory.

Searching by Type

You can search for files of specific types using the -type option. Some common file types include f for regular files and d for directories. Here’s an example:

1
find /path/to/directory -type f -name "*.txt"

This command will find only regular files (excluding directories and symbolic links) with the extension .txt in the specified directory.

Combining Expressions

You can combine different search expressions using logical operators to perform more complex searches. The most commonly used operators are -o (OR), -a (AND), and ! (NOT). For instance:

1
find /path/to/directory \( -name "*.jpg" -o -name "*.png" \) -size +1M

This command will search for files with the extensions .jpg or .png larger than 1 MB in the specified directory.

Actions

In addition to searching for files, you can perform various actions on the matched files. Here are a few commonly used actions:

Executing a Command

The -exec option allows you to execute a command on each matched file. For example:

1
find /path/to/directory -type f -name "*.txt" -exec ls -l {} \;

This command will execute the ls -l command on each file matching the criteria, displaying detailed information about each file.

Deleting Files

The -delete action removes files or directories that match the search criteria. Caution: This action is irreversible, so use it with caution.

1
find /path/to/directory -type f -name "temp.txt" -delete

This command will delete all files named temp.txt in the specified directory.

Conclusion

The Linux find command is a versatile tool for efficiently searching for files and directories based on different criteria. By combining various expressions and actions, you can narrow down your search, perform complex operations, and manage your files effectively.

This blog post provided an in-depth look at the find command, including numerous examples to ensure you have a solid understanding of its capabilities. Take advantage of this powerful command to streamline your file management tasks and boost your productivity as a programmer.


➡️ Interfacing ESP32 with External Sensors: Accelerometers, GPS, and more


⬅️ Low Power Deep Sleep Modes in ESP32: Exploring Power Saving Techniques


Go back to Posts.