Linux find Command
The Linux find command is used to search for files and directories under a specified directory.
It can use different options to filter and limit the search results.
Syntax
find [路径] [匹配条件] [动作]
Parameter Description :
Path is the directory path to search; it can be a directory or a file name, or multiple paths separated by spaces. If no path is given, the current directory is used by default.
expression is an optional argument used to specify search criteria, which can be file name, file type, file size, and so on.
There are as many as twenty or thirty options available in the match expression; the most commonly used ones are listed below:
-name pattern: search by file name, with support for wildcards*and?.-type type: search by file type, which can bef(regular file),d(directory),l(symbolic link), and so on.-size [+-]size[cwbkMG]: search by file size, with support for using+or-to indicate greater or less than a specified size; units can bec(bytes),w(words),b(blocks),k(KB),M(MB) orG(GB).-mtime days: search by modification time, with support for using+or-to indicate before or after a specified number of days; days is an integer representing the number of days.-user username: search by file owner.-group groupname: search by the group that owns the file.
**Actions: **optional, used to perform operations on matched files, such as deleting or copying.
The time-related parameters in the find command are as follows:
-amin n: find files accessed within n minutes.-atime n: find files accessed within n*24 hours.-cmin n: find files whose status changed (for example, permissions) within n minutes.-ctime n: find files whose status changed (for example, permissions) within n*24 hours.-mmin n: find files modified within n minutes.-mtime n: find files modified within n*24 hours.
Among these parameters, n can be a positive number, a negative number, or zero. A positive number means files modified or accessed within the specified time, a negative number means files modified or accessed before the specified time, and zero means files modified or accessed at the current point in time.
A positive number should mean before the time, and a negative number within the time.
For example: -mtime 0 finds files modified today, and -mtime -7 finds files modified more than a week ago.
Notes on the time parameter n:
-
+n: find files or directories older than n days.
-
-n: find files or directories whose attributes changed within n days.
-
n: find files or directories whose attributes changed exactly n days ago (on that specific day).
Example
Find the file named file.txt in the current directory:
find . -name file.txt
List all files with the .c extension in the current directory and its subdirectories:
# find . -name "*.c"
List all files in the current directory and its subdirectories:
# find . -type f
Find files larger than 1MB under the /home directory:
find /home -size +1M
Find files under /var/log that were modified more than 7 days ago:
find /var/log -mtime +7
Find files accessed in the past 7 days:
find /path/to/search -atime -7
Find files and directories whose status changed in the last 20 days under the current directory:
# find . -ctime 20
List all files in the current directory and its subdirectories that were updated 20 or more days ago:
# find . -ctime +20
Find regular files in /var/log whose change time is more than 7 days old, and ask before deleting them:
# find /var/log -type f -mtime +7 -ok rm {} \;
Find files in the current directory whose owner has read and write permissions, and whose group users and other users have read permission:
# find . -type f -perm 644 -exec ls -l {} \;
Find all regular files of length 0 in the system and list their full paths:
# find / -type f -size 0 -exec ls -l {} \;
Find and execute an operation (for example, deletion):
find /path/to/search -name "pattern" -exec rm {} \;
In this example, the -exec option lets you execute a command, {} is replaced by the matched file names, and ; marks the end of the command.
Linux Command Reference