Linux Guide: find


Mastering the find Command in Linux

The find command is a powerful tool for searching for files and directories within a directory tree in Linux. It allows users to locate files based on various criteria such as name, type, size, and modification date. Below, we’ll explore essential usage patterns and examples to help you make the most of this versatile command.

Basic Usage

To begin with, the basic syntax of the find command looks like this:

find [path] [options] [expression]

This command scans the specified path (or the current directory if none is provided) and applies the given search criteria.

Finding Files by Extension

One of the common tasks is to find files with a specific extension. The syntax for this is:

find root_path -name '*.ext'

Replace root_path with the desired directory and *.ext with the pertinent file extension.

Matching Multiple Patterns

You may need to match files against more than one pattern. This can be achieved with:

find root_path -path '*/path/*/*.ext' -or -name '*pattern*'

This command will locate files that meet either of the specified conditions.

To search for directories with a specific name while ignoring case differences, use:

find root_path -type d -iname '*lib*'

This will list directories containing the string “lib,” regardless of case.

Excluding Specific Paths

If you want to find files that match a certain pattern but need to exclude some paths, you can do so with:

find root_path -name '*.py' -not -path '*/site-packages/*'

This command locates Python files while excluding any located in the site-packages directory.

Searching by Size and Depth

To find files within a specific size range and limit the recursive depth, use:

find root_path -maxdepth 1 -size +500k -size -10M

This example finds files larger than 500 KB but smaller than 10 MB, searching only in the top-level directory.

Executing Commands on Found Files

You can run commands on each found file using the -exec option. For example, if you want to count lines in files with a specific extension:

find root_path -name '*.ext' -exec wc -l {} \;

The {} placeholder is replaced by the filename found.

Archiving Recently Modified Files

To find all files modified today and archive them, use:

find root_path -daystart -mtime -1 -exec tar -cvf archive.tar {} \+

This command creates a tar archive of files modified within the last 24 hours.

Deleting Empty Files or Directories

To search for and delete empty files or directories while displaying the results, use:

find root_path -type f -or -type d -empty -delete -print

This command helps clean up your file system by removing unneeded empty files and directories.

Conclusion

The find command is a robust utility that can help you efficiently locate and manipulate files on your system. Whether you need to find files by extension, match patterns, exclude certain paths, or execute commands based on your search results, mastering this command will enhance your productivity in a Linux environment. For more detailed documentation, you can refer to the manual at manned.org/find. Happy searching!

See Also