Writing in the front
Find is used frequently in Linux daily management, and proficiency in it can be very productive.
The syntax of find is simple, and there are only a few common parameters, such as -name, -type, -ctime, etc. For starters, look directly at the examples in Part 2, and refer to the find help documentation for more information about the parameters.
Find syntax is as follows:
Find (option)(parameter)
Common examples
Search by filename
Lists all files in the current directory and subdirectories
find .
Copy the code
Find the file named 11.png in the current directory
find . -name "11.png"
Copy the code
Find all JPG files in the current directory
find . -name "*.jpg"
Copy the code
Find JPG and PNG files in the current directory
find . -name "*.jpg" -o -name "*.png"
Copy the code
Find files in the current directory that do not end in PNG
find . ! -name "*.png"
Copy the code
Search by regular expression
Note: Regular representations are more complex than originally thought, supporting several types. You can refer to it here
Find PNG files with numeric names in the current directory.
find . -regex "\./*[0-9]+\.png"
Copy the code
Search by path
Find the file/path that contains WYSIWYg in the current directory.
find . -path "*wysiwyg*"
Copy the code
Search by file type
The value of type is used to filter file types.
- F Common files
- L sign connection
- D directory
- C character device
- B block device
- S a socket
- p Fifo
For example, find the files that contain WYSIWYG in the path of the current directory
find . -type f -path "*wysiwyg*"
Copy the code
Limit search depth
Find all PNGS in the current directory, not subdirectories.
find . -maxdepth 1 -name "*.png"
Copy the code
The corresponding minDepth option.
find . -mindepth 2 -maxdepth 2 -name "*.png"
Copy the code
Based on file size
Filter file size by -size. The supported file size units are as follows
- B — Block (512 bytes)
- C – byte
- W — word (2 bytes)
- K — kilobytes
- M — megabytes
- G — gigabytes
For example, find files in the current directory that are larger than 100 MB
find . -type f -size +100M
Copy the code
Based on access/modification/change time
The following time types are supported.
- Access time (-atime/ day, -amin/ min) : indicates the last access time.
- Modification time (-mtime/ day, -mmin/ minute) : indicates the time when a file is last modified.
- Change time (-ctime/ day, -cmin/ minute) : indicates the time when file data elements (such as permissions) are modified for the last time.
For example, find the files that have been modified within 1 day
find . -type f -mtime -1
Copy the code
Find the files that have been accessed in the last 1 week
find . -type f -atime -7
Copy the code
Move the log files that are more than a week old to/TMP /old_logs.
find . -type f -mtime +7 -name "*.log" -exec mv {} /tmp/old_logs \;
Copy the code
Note: {} is used in conjunction with the -exec option to match all files and is then replaced with the appropriate filename.
In addition, \; Indicates the end of the command. If the command is not added, the following message is displayed
find: -exec: no terminating ";" or "+"
Copy the code
According to the authority
This is done by -perm. For example, find the file whose permission is 777 in the current directory
find . -type f -perm 777
Copy the code
Find PHP files in the current directory that are not 644 privileges
find . -type f -name "*.php" ! -perm 644
Copy the code
According to the file owner
Find the file whose owner is root
find . -type f -user root
Copy the code
Locate the file whose group is root
find . -type f -group root
Copy the code
Execute the command when the file is found
-ok, and -exec. The difference is that -ok does a double check before executing the command, while -exec does not.
Let’s look at a real example. Delete all js files in the current directory. The effect of -ok is as follows, with a double check before deleting
➜ find find. -type f-name"*.js" -ok rm {} \;
"rm ./1.js"?
Copy the code
Try – exec. I just deleted it
find . -type f -name "*.js" -exec rm {} \;
Copy the code
Finding empty files
Examples are as follows
touch {1.9}.txt
echo "hello" > 1.txt
find . -empty
Copy the code