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 :
PathThis is the directory path to search. It can be a directory or file name, or multiple paths, separated by spaces. If no path is specified, it defaults to the current directory.
expressionThis is an optional parameter used to specify the search condition; it can be a file name, file type, file size, and so on.
There are as many as twenty or thirty options available in the matching conditions. The most commonly used ones are listed below:
-name pattern: Search by file name; wildcards are supported.*and?。-type type: Search by file type; it can bef(regular files),d(directories),l(symbolic links), etc.-size [+-]size[cwbkMG]: Search by file size; it supports using+or-to indicate greater than or less than the specified size; the unit can bec(bytes),w(words),b(block count),k(KB)、M(MB), orG(GB)。-mtime days: Search by modification time; it supports using+or-to indicate before or after the specified number of days; days is an integer representing the number of days.-user username: Search by file owner.-group groupname: Search by file group.
Actions:Optional; used to perform operations on the matched files, such as deletion, copying, etc.
The time-related parameters in the find command are as follows:
-
-amin n: Find files that were accessed within n minutes. -
-atime n: Find files that were accessed within n*24 hours. -
-cmin n: Find files whose status changed within n minutes (e.g., permissions). -
-ctime n: Find files whose status changed within n*24 hours (e.g., permissions). -
-mmin n: Find files that were modified within n minutes. -
-mtime n: Find files that were modified within n*24 hours.
In these parameters, n can be a positive number, a negative number, or zero. A positive number indicates files modified or accessed within the specified time, a negative number indicates files modified or accessed before the specified time, and zero indicates files modified or accessed at the current point in time.
Positive numbers should represent before the specified time, and negative numbers represent within the specified time.
For example:-mtime 0Indicates searching for files modified today,-mtime -7Indicates searching for files modified more than a week ago.
Explanation of the time n parameter:
+n: Find files or directories older than n days.
-
-n: Find files or directories whose attributes were changed within n days.
-
n: Find files or directories whose attributes were changed n days ago (on that specified day).
Examples
Find the file named file.txt in the current directory:
find . -name file.txt
List all files in the current directory and its subdirectories with the suffix.cand list the files:
# find . -name "*.c"
List all files in the current directory and its subdirectories:
# find . -type f
Find files larger than 1MB in the /home directory:
find /home -size +1M
Find files in the /var/log directory modified 7 days ago:
find /var/log -mtime +7
Find files accessed within the past 7 days:
find /path/to/search -atime -7
Find files and directories whose status has changed within the last 20 days in the current directory:
# find . -ctime 20
List all files in the current directory and its subdirectories that were updated 20 days ago or earlier:
# find . -ctime +20
Find regular files in the /var/log directory whose modification time is more than 7 days ago, and ask for confirmation before deleting them:
# find /var/log -type f -mtime +7 -ok rm {} \;
Find files in the current directory where the file owner has read and write permissions, and the group users and other users have read permission:
# find . -type f -perm 644 -exec ls -l {} \;
Find all regular files with a file length of 0 in the system, and list their full paths:
# find / -type f -size 0 -exec ls -l {} \;
Find and perform operations (e.g., delete):
find /path/to/search -name "pattern" -exec rm {} \;
In this example,-execthe option allows you to execute a command,{}will be replaced by the matched file name,\;indicates the end of the command.
Other Extensions
Linux Command Encyclopedia