Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Finding Files on Linux with `find`

1 min read .
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]
  • path specifies where the search starts. Use . for the current directory.
  • expression defines 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 +500M

This finds files larger than 500 MiB. Use -size -100M for files smaller than 100 MiB.

Find Recently Modified Files

find . -type f -mtime -7

This 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.

Related Posts

chevron-up