Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux basic learning files to find common uses of find

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

preface

In the daily management of linux, the use of find frequency is very high, proficient in improving work efficiency is very helpful.

The syntax of find is relatively simple, and there are only a few commonly used parameters, such as-name, -type, -ctime, etc. Beginners can see the examples in Part II directly. For further information on parameters, please refer to the help document of find.

Find syntax is as follows:

find(options)(parameters)

conventional example

Search by file name

List all files in the current directory and subdirectories

find .

Find a file named 11.png in the current directory

find . -name "11.png"

Find all jpg files in the current directory

find . -name "*.jpg"

Find jpg files and png files in the current directory

find . -name "*.jpg" -o -name "*.png"

Find files in the current directory that do not end in png

find . ! -name "*.png"

Search by regular expression

Note: Regular expressions are more complex than originally thought and support several types. can be found here

Find png files with numeric filenames in the current directory.

find . -regex "\./* [0-9]+\.png"

Find by route

Find the file/path in the current directory that contains wysiwyg.

find . -path "*wysiwyg*"

Search by file type

Filter file types by-type.

f Common file l Symbolic link d Directory c Character device b Block device s Socket p Fifo

For example, find the file containing wysiwyg in the current directory

find . -type f -path "*wysiwyg*"

Limit search depth

Find all png in the current directory, excluding subdirectories.

find . -maxdepth 1 -name "*.png"

The same is true for mindepth options.

find . -mindepth 2 -maxdepth 2 -name "*.png"

according to the size of the file

Filter file size by-size. Supported file size units are as follows

b --block (512 bytes) c --byte w --word (2 bytes) k --kilobyte M --megabyte G --gigabyte

For example, find files larger than 100 MB in the current directory

find . -type f -size +100M

Based on access/modification/change time

The following time types are supported.

Access time (-atime/day, -amin/minute): The user's last access time. Modified time (-mtime/day, -mmin/minute): The last time the file was modified. Change time (-ctime/day, -cmin/minute): the last modification time of file data elements (such as permissions).

For example, find files that have been modified in one day.

find . -type f -mtime -1

Find files accessed in the last week

find . -type f -atime -7

Move log files older than a week from the log directory to/tmp/old_logs.

find . -type f -mtime +7 -name "*.log" -exec mv {} /tmp/old_logs \;

Note: {} is used in conjunction with the-exec option to match all files and is then replaced with the appropriate file name.

In addition,\; used to indicate the end of the command, if not added, there will be the following prompt

find: -exec: no terminating ";" or "+"

according to permission

This is done with-perm. For example, find files with permissions 777 in the current directory

find . -type f -perm 777

Find php files with permissions other than 644 in the current directory

find . -type f -name "*.php" ! -perm 644

According to file owner

Find files owned by root

find . -type f -user root

Find files in the root group

find . -type f -group root

Find the file and execute the command

This is done by-ok, and-exec. The difference is that-ok checks twice before executing the command, and-exec doesn't.

Consider a practical example. Delete all js files in the current directory. The effect of using-ok is as follows. There is a second confirmation before deletion.

➜ find find . -type f -name "*.js" -ok rm {} \;"rm ./ 1.js"?

Try-exec. Just deleted it.

find . -type f -name "*.js" -exec rm {} \;

Find empty files

following are examples

touch {1.. 9}.txtecho "hello" > 1.txtfind . -empty

Find files modified two days ago:

find . -type f -mtime -2

Find files modified in 3 days:

find -ctime -3

Find command Find empty files older than 6 days Independent query command:

find /data/backup -ctime +6 -exec rm -f {} \;

Delete files in the/data/backup directory that have been modified for more than 6 days.

find /data/backup -type d -empty -exec rmdir {} \; >/dev/null 2>&1

Delete the empty folder in/data/backup directory and output correct and error messages to empty.

Find empty files older than 6 days:

find ./ -type d -empty -ctime +6

To find files by modification time, use the option-mtime:

find /home/admin -mtime -1 #Find files in the/home/admin directory modified within 1 day find /home/admin -name *.txt -mtime -1 #Find files in the/home/admin directory modified within 1 day with a name ending in.txt

summary

The above is the whole content of this article, I hope the content of this article has a certain reference value for everyone's study or work, if you have questions, you can leave a message to exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report