find Command Cheatsheet
The find command is used to recursively search for files and directories within a specified location based on a variety of criteria. It is one of the most powerful and versatile commands for file system navigation.
Common Syntax
find [PATH] [OPTIONS] [EXPRESSION]The `PATH` is the directory where the search starts. `OPTIONS` control the search behavior, and `EXPRESSION` is the set of criteria used to filter the results.
Search by Name
| Command | Description |
|---|---|
find . -name "file.txt" | Finds files named exactly "file.txt" in the current directory and its subdirectories. Use quotes to prevent shell globbing. |
find . -name "*.log" | Finds all files with a `.log` extension. The asterisk (`*`) is a wildcard. |
find . -iname "file.txt" | Case-insensitive version of the `-name` option. |
Search by Type
| Command | Description |
|---|---|
find . -type f | Finds only regular files. |
find . -type d | Finds only directories. |
find . -type l | Finds only symbolic links. |
Combining Search Criteria
By default, `find` uses a logical AND (`-a`) operator between expressions. You can use `-o` for OR.
| Command | Description |
|---|---|
find . -name "*.txt" -o -name "*.log" | Finds files that end with either `.txt` OR `.log`. |
find . -type f -name "*.txt" | Finds files that are both a regular file AND end with `.txt`. |
Executing a Command on Results
The `-exec` option allows you to execute a command on each file found by `find`.
find . -name "*.txt" -exec rm {} \;This command finds all `.txt` files and then executes `rm` on each one. The `{}` is a placeholder for the current filename, and `\;` marks the end of the `-exec` command.