2024-09-02 16:42:08 -06:00
|
|
|
# File Searching using find
|
|
|
|
|
|
|
|
|
|
##1. Basic Syntax:
|
|
|
|
|
The basic syntax of the find command is:
|
2024-09-07 09:29:54 -06:00
|
|
|
`find [path] [expression]` # Where [path] is the directory you want to search in, and [expression] defines the search criteria.
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 2. Searching by Name:
|
|
|
|
|
- To find files by name:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -name "filename"` # Perform a case-sensitive search "-name"
|
2024-09-02 16:42:08 -06:00
|
|
|
- Use wildcards for pattern matching:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -name "*.txt"`
|
2024-09-02 16:42:08 -06:00
|
|
|
- For case-insensitive search:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -iname "filename"` # Perform a insensitive case search.
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 3. Searching by Type:
|
|
|
|
|
- Find only directories:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -type d`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find only regular files:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -type f`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find symbolic links:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -type l`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 4. Searching by Size:
|
|
|
|
|
- Find files larger than 100MB:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -size +100M`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find files smaller than 1KB:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -size -1k`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find files exactly 50 bytes:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -size 50c`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 5. Searching by Time:
|
|
|
|
|
- Find files modified in the last 7 days:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -mtime -7`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find files accessed more than 30 days ago:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -atime +30`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find files whose status was changed within the last hour:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -cmin -60`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 6. Searching by Permissions:
|
|
|
|
|
- Find files with exact permissions:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -perm 644`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Find files with at least these permissions:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -perm -644`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 7. Logical Operators:
|
|
|
|
|
- AND operation (implicit):
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -name "*.txt" -size +1M`
|
2024-09-02 16:42:08 -06:00
|
|
|
- OR operation:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search \( -name "*.txt" -o -name "*.pdf" \)`
|
2024-09-02 16:42:08 -06:00
|
|
|
- NOT operation:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search ! -name "*.txt"`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 8. Actions:
|
|
|
|
|
- Print results (default action):
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -name "*.txt" -print`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Execute a command on found files:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -name "*.txt" -exec cat {} \;`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Delete found files (use with caution):
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -name "*.tmp" -delete`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 9. Limiting Depth:
|
|
|
|
|
- Search only in the current directory:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -maxdepth 1 -name "*.txt"`
|
2024-09-02 16:42:08 -06:00
|
|
|
- Limit search to 3 levels deep:
|
2024-09-07 09:29:54 -06:00
|
|
|
- `find /path/to/search -maxdepth 3 -name "*.txt"`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 10. Performance Optimization:
|
2024-09-07 09:29:54 -06:00
|
|
|
- Use '-xdev' to stay on one filesystem:
|
|
|
|
|
- `find /path/to/search -xdev -name "*.txt"`
|
|
|
|
|
- Optimize for large directories:
|
|
|
|
|
- `find /path/to/search -name "*.txt" -fprintf /dev/stdout '%p\0' | xargs -0 process`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
|
|
|
|
## 11. Advanced Examples:
|
2024-09-07 09:29:54 -06:00
|
|
|
- Find all empty directories:
|
|
|
|
|
- `find /path/to/search -type d -empty`
|
|
|
|
|
- Find all files owned by a specific user:
|
|
|
|
|
- `find /path/to/search -user username`
|
|
|
|
|
- Find files and compress them:
|
|
|
|
|
- `find /path/to/search -name "*.log" -exec gzip {} +`
|
2024-09-02 16:42:08 -06:00
|
|
|
|
2024-09-07 09:29:54 -06:00
|
|
|
## 12. Find Alternatives:
|
|
|
|
|
**Using `locate`**:
|
|
|
|
|
- The `locate` command quickly searches for files and directories by their names. Here's the basic syntax:
|
|
|
|
|
```
|
|
|
|
|
locate {filename}
|
|
|
|
|
```
|
|
|
|
|
- For example, to locate a file named `resolv.conf`, you can run:
|
|
|
|
|
```
|
|
|
|
|
locate resolv.conf
|
|
|
|
|
```
|