<span>find</span> command is a powerful tool in Unix and Unix-like systems (such as Linux) used to search for files and directories within a directory tree. It offers various options for complex searches, allowing filtering based on file name, type, time, size, and more. Below is a detailed introduction to the <span>find</span> command, including commonly used options and examples.
Basic Syntax
find [path] [options] [test conditions] [actions]
-
path: Specifies the directory path to search. It can be one or more paths, or the current directory (
<span>.</span>). -
options: Controls the behavior of the
<span>find</span>command, such as ignoring errors, displaying help information, etc. -
test conditions: Conditions used to match files, such as file name, type, modification time, etc.
-
actions: Actions to perform on matching files, such as printing paths, deleting files, etc.
Common Options
-
<span>-name</span>: Matches files based on file name. -
Example:
<span>find /path -name "*.txt"</span>searches for all files with the extension<span>.txt</span>in the<span>/path</span>directory. -
<span>-iname</span>: Similar to<span>-name</span>, but case-insensitive. -
Example:
<span>find /path -iname "*.jpg"</span>searches for all files with the extension<span>.jpg</span>(case insensitive) in the<span>/path</span>directory. -
<span>-type</span>: Matches files based on file type. -
<span>f</span>: regular file -
<span>d</span>: directory -
<span>l</span>: symbolic link -
Example:
<span>find /path -type d</span>searches for all directories in the<span>/path</span>directory. -
<span>-size</span>: Matches files based on file size. -
Example:
<span>find /path -size +1M</span>searches for files larger than 1MB in the<span>/path</span>directory. -
Units such as
<span>c</span>(bytes),<span>k</span>(kilobytes),<span>M</span>(megabytes), etc. can be used. -
<span>-mtime</span>: Matches files based on the last modification time. -
<span>-mtime n</span>: Matches files modified<span>n</span>days ago. -
<span>-mtime +n</span>: Matches files modified more than<span>n</span>days ago. -
<span>-mtime -n</span>: Matches files modified within the last<span>n</span>days. -
Example:
<span>find /path -mtime -7</span>searches for files modified in the last 7 days. -
<span>-ctime</span>: Matches files based on the last change time of file metadata. -
Example:
<span>find /path -ctime +30</span>searches for files whose metadata was changed more than 30 days ago. -
<span>-atime</span>: Matches files based on the last access time. -
Example:
<span>find /path -atime -1</span>searches for files accessed in the last 24 hours. -
<span>-mmin</span>: Matches files based on the last modification time in minutes. -
Example:
<span>find /path -mmin +60</span>searches for files modified more than 60 minutes ago. -
<span>-cmin</span>: Matches files based on the last change time of file metadata in minutes. -
Example:
<span>find /path -cmin -30</span>searches for files whose metadata was changed in the last 30 minutes. -
<span>-amin</span>: Matches files based on the last access time in minutes. -
Example:
<span>find /path -amin +10</span>searches for files accessed more than 10 minutes ago.
Actions
-
<span>-print</span>: The default action, prints the paths of matching files. -
Example:
<span>find /path -name "*.log" -print</span>prints the paths of all<span>.log</span>files. -
<span>-exec</span>: Executes a command on matching files. -
Example:
<span>find /path -name "*.tmp" -exec rm -f {} \;</span>deletes all<span>.tmp</span>files.<span>{}</span>represents the current matching file, and<span>\;</span>ends the command. -
<span>-delete</span>: Deletes matching files. Note: Use with caution, as files will be permanently deleted. -
Example:
<span>find /path -name "*.bak" -delete</span>deletes all<span>.bak</span>files. -
<span>-print0</span>: Separates paths with a null character (<span>\0</span>), suitable for file names containing spaces or special characters. -
Example:
<span>find /path -type f -print0 | xargs -0 rm</span>deletes all matching files. -
<span>-prune</span>: Excludes the specified directory and its subdirectories from the search. -
Example:
<span>find /path -type d -name "ignore" -prune -o -type f -print</span>searches for all files in the<span>/path</span>directory, but excludes the directory named<span>ignore</span>and its subdirectories.
Examples
-
Find all
<span>.log</span>files in the current directory:find . -name "*.log" -
Find files larger than 100MB in the
<span>/var/log</span>directory:find /var/log -size +100M -
Find files modified more than 7 days ago and delete them:
find /path -mtime +7 -exec rm -f {} \; -
Find files accessed in the last 30 minutes and print their paths:
find /path -amin -30 -print -
Find and delete all
<span>.tmp</span>files (use with caution):find /path -name "*.tmp" -delete
Conclusion
<span>find</span> command is a powerful tool for searching and manipulating files within a directory tree. By mastering the various options and actions of the <span>find</span> command, you can efficiently manage and handle files in your system.
Link: https://www.cnblogs.com/daikaiqiang/p/18356649
(Copyright belongs to the original author, please delete if infringing)
WeChat group
To facilitate better communication regarding operation and maintenance and related technical issues, a WeChat group has been created. Friends who want to join the group can scan the QR code below to add me as a friend (note: join group).

Blog
CSDN Blog: https://blog.csdn.net/qq_25599925
Juejin Blog: https://juejin.cn/user/4262187909781751
Long press to recognize the QR code to visit the blog website for more quality original content.