Linux system powerful search command find usage

Find is the most commonly used command under Linux. The find command searches for files in the directory structure and performs specified operations. It is very useful for operation and maintenance to master its form and usage.

find common parameters

-exec:find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } ;,注意{ }和\;之间的空格。
-name filename 查找名为filename的文件
-mtime -n +n 按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime -n +n 按文件访问时间来查
-ctime -n +n 按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-type b/d/c/p/l/f 查是块设备、目录、字符设备、管道、符号链接、普通文件
-size n[c] 查长度为n块[或n字节]的文件
-mindepth -maxdepth 选项允许您指定您希望 find 搜索深入到目录树的哪一级别

Case Sharing
Today, when writing a MySQL database backup script, I encountered a difficult problem. It was necessary to clean up the backup files 14 days ago. You may think this is not a very simple matter. Use find to find out, and then add xargs rm -rf to delete it automatically.

But things are not so simple. Let me introduce your requirements. I want to find the 14-day directory under /home/mysql/backup/physical and delete it.


[mysql@localhost physical]$ pwd
/home/mysql/backup/physical

[mysql@localhost physical]$ ls -l
total 0
drwxr-x--- 3 mysql mysql 31 Mar 23 02:30 202003230230
drwxr-x--- 3 mysql mysql 31 Mar 23 02:33 202003230233

Use ordinary find parameters to try the effect


[mysql@localhost physical]$ find /home/mysql/backup/physical -type d -mtime +14
/home/mysql/backup/physical
/home/mysql/backup/physical/202003230230
/home/mysql/backup/physical/202003230230/base_202003230230
/home/mysql/backup/physical/202003230230/base_202003230230/mysql
/home/mysql/backup/physical/202003230230/base_202003230230/sys
/home/mysql/backup/physical/202003230230/base_202003230230/testdb
/home/mysql/backup/physical/202003230230/base_202003230230/performance_schema
/home/mysql/backup/physical/202003230233
/home/mysql/backup/physical/202003230233/base_202003230233
/home/mysql/backup/physical/202003230233/base_202003230233/mysql
/home/mysql/backup/physical/202003230233/base_202003230233/sys
/home/mysql/backup/physical/202003230233/base_202003230233/testdb
/home/mysql/backup/physical/202003230233/base_202003230233/performance_schema

But I know that to find the following two directories, there is no need to find the lower-level directories. Obviously, find finds more directories.


[mysql@localhost physical]$ ll
total 0
drwxr-x--- 3 mysql mysql 31 Mar 23 02:30 202003230230
drwxr-x--- 3 mysql mysql 31 Mar 23 02:33 202003230233

What to do? When I use man find to see the usage, I found that there are 2 parameters -mindepth -maxdepth, which can control the level of query. It is very useful to solve this requirement. Let's try the effect


[mysql@localhost physical]$ find /home/mysql/backup/physical -mindepth 1 -maxdepth 1 -type d -mtime +14
/home/mysql/backup/physical/202003230230
/home/mysql/backup/physical/202003230233

So far, my needs have been perfectly solved, just add xargs rm -rf

[mysql@localhost physical]$ find /home/mysql/backup/physical -mindepth 1 -maxdepth 1 -type d -mtime +14| xargs rm -rf

Have you learned this skill?

Guess you like

Origin blog.51cto.com/15061930/2642098