Finding Files on Linux with `find`
When a directory tree becomes large and deeply nested, searching manually is inefficient. Linux provides the find command for locating files and directories by name, size, type, modification time, and many other properties.
1. Basic Syntax
find [path] [expression]pathspecifies where the search starts. Use.for the current directory.expressiondefines the filters and actions.
2. Practical Examples
Find a File by Name
find . -type f -name "example.txt"This searches the current directory and all subdirectories for a regular file named example.txt.
Find Files by Extension
find . -type f -name "*.jpg"Use -iname instead of -name for a case-insensitive match.
Find Large Files
find . -type f -size +500MThis finds files larger than 500 MiB. Use -size -100M for files smaller than 100 MiB.
Find Recently Modified Files
find . -type f -mtime -7This finds files modified within roughly the last seven 24-hour periods. For minute-level filtering, use -mmin.
Find Directories
find . -type d -name "images"3. Run a Command on Matches
find can execute a command for every matching path. For example:
find . -type f -name "*.log" -exec rm -f -- {} \;Here, {} is replaced with each matching filename. Because deletion is destructive, preview the matches first by running the same find command without -exec.
When the command supports multiple paths at once, {} + is often more efficient:
find . -type f -name "*.log" -exec ls -lh -- {} +Conclusion
find is one of the most useful Linux filesystem tools. By combining filters such as -type, -name, -size, and -mtime, you can locate exactly the files you need and optionally process the results directly.