🌟 Why Use find?
Imagine a server running for 3 years, and the boss asks to clean up log files larger than 100MB. If you were to search through them one by one using a graphical interface, you might end up working late into the night… However, with the find command, it only takes one line:
find /var/log -name "*.log" -size +100M -delete
🛠️ Basic Syntax and Core Parameters
find [search_path] [conditions] [actions]
Search Path: defaults to the current directory (.), supports absolute paths (/) or user directories (~)
Conditions: filename, type, size, time, etc.
Actions: print path (default), delete, execute command, etc.
📂 Searching by Name and Type
✨ Precisely Locate Filenames
# Case-sensitive search for passwd filefind / -name "passwd"# Case-insensitive search for JPG filesfind ~ -iname "*.JPG"# Regular expression matching (note the escape)find /data -regex ".*\.(jpg|png)$"
📁 Filtering File Types
# Find directoriesfind ~ -type d# Find symbolic linksfind /usr/local/bin -type l# Find regular filesfind /tmp -type f
⏳ Time and Size Filtering
🕒 Searching by Modification Time
# Files modified more than 7 days ago (cleaning old logs)find /var/log -mtime +7 -delete# Files modified in the last 3 daysfind ~/docs -mtime -3# Files newer than a specific filefind /data -newer /etc/config.txt
💾 Filtering by File Size
# Files larger than 100MBfind /data -size +100M# Files smaller than 1KBfind ~ -size -1k# Files exactly 512 bytesfind /dev -size 512c
🔐 Permission and Owner Search
🔑 Searching by Permissions
# Exact match for 777 permissionsfind / -perm 777# At least one user has execute permissionfind /bin -perm /u+x
👤 Searching by Owner / Group
# Find files owned by user yangfind /home -user yang# Find files belonging to the developers groupfind /data -group developers
🚀 Advanced Techniques and Practical Applications
🧩 Combining Conditions
# Find PDFs larger than 50MB, excluding backup directoriesfind /data -name "*.pdf" -size +50M -not -path "*backup*"# Directories or files modified in the last 7 daysfind . \( -type d -o -mtime -7 \)
🛠️ Executing Batch Operations
# Batch change permissionsfind . -type f -name "*.sh" -exec chmod 755 {} \;# Safe delete (preview first)find /tmp -type f -mtime +30 -ok rm {} \;# Parallel processing with xargsfind . -type f -print | xargs -n 100 chown yang
📝 Quick Reference Table
| Function | Command Example |
|---|---|
| Search by Name | <span><span>find /path -name "pattern"</span></span> |
| Search by Type | <span><span>find ~ -type d</span></span> (directory)/<span><span>find ~ -type l</span></span> (symbolic link) |
| Search by Time | <span><span>find /var/log -mtime +7</span></span> (7 days ago)/<span><span>find ~ -mtime -3</span></span> (within 3 days) |
| Search by Size | <span><span>find /data -size +100M</span></span> (>100MB)/<span><span>find ~ -size -1k</span></span> (<1KB) |
| Search by Permissions | <span><span>find / -perm 777</span></span> (exact permissions)/<span><span>find /bin -perm /u+x</span></span> (at least one execute bit) |
| Batch Operations | <span><span>find . -name "*.bak" -exec rm {} \;</span></span> (delete) |
| Performance Optimization | <span><span>find /data -maxdepth 2 -name "*.log"</span></span> (limit depth) |
