Linux Comm Find
# Linux find Command
[ Linux Command Manual](#)
The Linux find command is used to search for files and directories in 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 file name, or multiple paths separated by spaces. If no path is specified, it defaults to the current directory.
**expression** is an optional parameter used to specify the search conditions. It can be file name, file type, file size, etc.
There are as many as twenty or thirty options that can be used in the matching conditions. The most commonly used ones are listed below:
* `-name pattern`: Search by file name, supports wildcards `*` and `?`.
* `-type type`: Search by file type, can be `f` (regular file), `d` (directory), `l` (symbolic link), etc.
* `-size [+-]size`: Search by file size, supports using `+` or `-` to indicate greater than or less than the specified size. Units can be `c` (bytes), `w` (words), `b` (blocks), `k` (KB), `M` (MB), or `G` (GB).
* `-mtime days`: Search by modification time, 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.
**action:** Optional, used to perform operations on matched files, such as deleting, copying, etc.
The parameters used for time in the find command are as follows:
* `-amin n`: Find files accessed within the last n minutes.
* `-atime n`: Find files accessed within the last n*24 hours.
* `-cmin n`: Find files whose status changed within the last n minutes (e.g., permissions).
* `-ctime n`: Find files whose status changed within the last n*24 hours (e.g., permissions).
* `-mmin n`: Find files modified within the last n minutes.
* `-mtime n`: Find files modified within the last n*24 hours.
In these parameters, n can be a positive number, 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 time point.
**A positive number should indicate before the time, a negative number indicates within the time.**
For example: -mtime 0 means to find files modified today, -mtime -7 means to find files modified more than a week ago.
Explanation of the time n parameter:
* +n: Find files or directories older than n days ago.
* -n: Find files or directories whose attributes changed within n days.
* n: Find files or directories whose attributes changed on the nth day ago (that specific day).
### Examples
Find a file named file.txt in the current directory:
find . -name file.txt
List all files with the extension .c 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 in the /home directory:
find /home -size +1M
Find files modified more than 7 days ago in the /var/log directory:
find /var/log -mtime +7
Find files accessed in the past 7 days:
find /path/to/search -atime -7
Find files and directories in the current directory whose status changed in the last 20 days:
# find . -ctime 20
List all files updated 20 days ago or earlier in the current directory and its subdirectories:
# find . -ctime +20
Find regular files in the /var/log directory with a change time more than 7 days ago, and ask 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 file's group users and other users have read permissions:
# find . -type f -perm 644 -exec ls -l {} ;
Find all regular files with a size of 0 in the system and list their full paths:
# find / -type f -size 0 -exec ls -l {} ;
Find and execute an operation (e.g., delete):
find /path/to/search -name "pattern" -exec rm {} ;
In this example, the -exec option allows you to execute a command, {} will be replaced by the matched file name, and ; indicates the end of the command.
[ Linux Command Manual](#)
YouTip