LWM-Linux/06 - Linux File Operations/File Searching (find).md

90 lines
3.1 KiB
Markdown
Raw Normal View History

# 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.
## 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"
- Use wildcards for pattern matching:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -name "*.txt"`
- For case-insensitive search:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -iname "filename"` # Perform a insensitive case search.
## 3. Searching by Type:
- Find only directories:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -type d`
- Find only regular files:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -type f`
- Find symbolic links:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -type l`
## 4. Searching by Size:
- Find files larger than 100MB:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -size +100M`
- Find files smaller than 1KB:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -size -1k`
- Find files exactly 50 bytes:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -size 50c`
## 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`
- Find files accessed more than 30 days ago:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -atime +30`
- Find files whose status was changed within the last hour:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -cmin -60`
## 6. Searching by Permissions:
- Find files with exact permissions:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -perm 644`
- Find files with at least these permissions:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -perm -644`
## 7. Logical Operators:
- AND operation (implicit):
2024-09-07 09:29:54 -06:00
- `find /path/to/search -name "*.txt" -size +1M`
- OR operation:
2024-09-07 09:29:54 -06:00
- `find /path/to/search \( -name "*.txt" -o -name "*.pdf" \)`
- NOT operation:
2024-09-07 09:29:54 -06:00
- `find /path/to/search ! -name "*.txt"`
## 8. Actions:
- Print results (default action):
2024-09-07 09:29:54 -06:00
- `find /path/to/search -name "*.txt" -print`
- Execute a command on found files:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -name "*.txt" -exec cat {} \;`
- Delete found files (use with caution):
2024-09-07 09:29:54 -06:00
- `find /path/to/search -name "*.tmp" -delete`
## 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"`
- Limit search to 3 levels deep:
2024-09-07 09:29:54 -06:00
- `find /path/to/search -maxdepth 3 -name "*.txt"`
## 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`
## 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-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
```