Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux find Command

Linux find Command

Linux Command Reference Linux Command Reference

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:

**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:

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:

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 Linux Command Reference