find
It is the most powerful file search tool on Linux system and supportsNested traversal, conditional filtering, and execution of actions. The following analyzes the core usage through scene classification, covering efficient search, file management and advanced skills:
1. Basic search mode
1. Search by file name (exact/fuzzy matching)
<BASH>
find /path -name "*.log" # Exact match .log suffix (case sensitive)find /home -iname "*.TXT" # Fuzzy match .txt suffix (ignoring case)find . -name "data_[0-9].csv" # Use wildcards [] ? * match
2. Exclude specified directory/file
find /var/log -path "/var/log/nginx" -prune -o -name "*.log" # Exclude nginx directoryfind . -not -name "*.tmp" # Exclude all .tmp document
2. Filter according to file type
use-type
Filter file types:
-
f
: General files -
d
: Table of contents -
l
: Symbol link -
s
: Sockets -
p
: Named Pipeline
find /opt -type d -name "config" # Search for a directory called configfind ~/ -type f -empty # Find the empty file under the current userfind /tmp -type l -ls # List /tmp All symbolic link details below
3. Time dimension search
Filter by time (file modification time-mtime
/ Access time-atime
):
-
+n
: n days ago -
-n
: In the last n days -
n
: Just n days ago
find . -mtime -7 # Find files modified in the past 7 daysfind /var/log -mtime +30 -name "*.log" # search 30 Log files from day one
Accurate control at minute levels (-mmin
):
find /tmp -mmin -10 # Find the past 10 Files modified within minutes
4. File size search
use-size
(Unit:c
= bytes,k
=KB, M
=MB, G
=GB):
-
+n
: greater than n -
-n
: less than n -
n
: equal to n
find / -size +100M # Global search for files greater than 100MB (use the root directory with caution)find ~/Downloads -size -5k # Find Downloads Smaller than 5KB Files
5. Permissions and ownership filtering
1. Search by permission bit (numeric or symbol mode)
find . -perm 644 # Exactly match files with permissions of 644find /etc -perm -u=r # User-readable file (permissions include r)find /bin -perm /u=s # Include SUID Files
2. Filter by user/group
find /var -user www-data # Files belonging to www-data usersfind /home -group dev # belong dev Group of files
6. Combination conditions and logical operations
-
-a
(-and
): Logical and (default) -
-o
(-or
) : Logical or -
!
(-not
) : Logical non - use
()
Grouping (requires escape)
find /data \( -name "*.jpg" -o -name "*.png" \) -size +2M # Find JPG or PNG and greater than 2MBfind . -type f ! -name "*.tmp" # Exclude all .tmp document
7. Perform subsequent actions (-exec and xargs)
1. Execute the command directly (-exec
)
find . -name "*.bak" -exec rm -f {} \; # Delete all .bak files (execute after confirmation)find /var/log -type f -mtime +30 -exec gzip {} \; # compression 30 Log files from day one
2. Combinationxargs
Improve efficiency
find /tmp -name "core.*" -print0 | xargs -0 rm -f # Safe processing of file names containing spaces
3. Delete files (built-in action)
find . -type f -name "*.tmp" -delete # Delete directly(-delete Must be put at the end)
8. Advanced search scenarios
1. Find small files that occupy inode
find /path -type f -size +0c -links 1 -exec ls -i {} \; # Non-hard link standalone files
2. Find duplicate files (according to MD5)
find . -type f -exec md5sum {} + | sort | uniq -w32 -dD # Generate checksum comparison duplicates
9. Safety inspection skills
1. Search for suspicious SUID/SGID files
find / -perm /4000 -user root 2>/dev/null # SUID and the owner is rootfind / -perm /2000 -group root 2>/dev/null # SGID And the group is root
2. Find global writable files
find / -xdev -type f -perm -0002 ! -perm -1000 # Ignore files in sticky bit directory
10. Performance optimization suggestions
Limited search depth:-maxdepth
and-mindepth
find /var/log -maxdepth 2 -name "*.log" # Search only two-layer directory
Skip specific file systems:-xdev
(Not cross file system)
find / -xdev -name "lost+found" # Search only on the current file system
Summarize
-
Core mode:
find [path] [condition] [action]
, flexibly combine conditions to achieve accurate search. -
Safety first: It is recommended to use it before deletion or modification.
-print
or-ls
Confirm the target file. -
Efficiency priority: In high load scenarios, try to minimize full disk scanning and use it reasonably
-xargs
Improve performance.
This is the end of this article about the complete guide to Linux find commands. For more related contents of Linux find commands, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!