---
title: "find"
author: "east_java"
date: "2013-12-26T17:13:44Z"
tags: []
categories: ["Web前端"]
slug: "find"
draft: false
lastmod: "2013-12-26T17:13:44Z"
id: 1995920
url: "https://japi.iteye.com/blog/1995920"
---

find是我们很常用的一个Linux命令，但是我们一般查找出来的并不仅仅是看看而已，还会有进一步的操作，这个时候exec的作用就显现出来了。 

exec解释：

-exec  参数后面跟的是command命令，它的终止是以;为结束标志的，所以这句命令后面的分号是不可缺少的，考虑到各个系统中分号会有不同的意义，所以前面加反斜杠。

{}   花括号代表前面find查找出来的文件名。

使用find时，只要把想要的操作写在一个文件里，就可以用exec来配合find查找，很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前，最好先用ls命令看一下，确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本，然后是一对儿{ }，一个空格和一个\\，最后是一个分号。为了使用exec选项，必须要同时使用print选项。如果验证一下find命令，会发现该命令只输出从当前路径起的相对路径及文件名。

实例1：ls -l命令放在find命令的-exec选项中 

命令：

find . -type f -exec ls -l {} \\;

输出： 

\[root@localhost test]# find . -type f -exec ls -l {} \\; 

-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log

-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log

-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log

-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log

-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

\[root@localhost test]#

说明： 

上面的例子中，find命令匹配到了当前目录下的所有普通文件，并在-exec选项中使用ls -l命令将它们列出。

实例2：在目录中查找更改时间在n日以前的文件并删除它们

命令：

find . -type f -mtime +14 -exec rm {} \\; 

输出：

\[root@localhost test]# ll

总计 328

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     33 10-28 16:54 log2013.log

-rw-r--r-- 1 root root    127 10-28 16:51 log2014.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log\_link.log -&gt; log.log

-rw-r--r-- 1 root root     25 10-28 17:02 log.log

-rw-r--r-- 1 root root     37 10-28 17:07 log.txt

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 10-28 14:47 test3

drwxrwxrwx 2 root root   4096 10-28 14:47 test4

\[root@localhost test]# find . -type f -mtime +14 -exec rm {} \\;

\[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log\_link.log -&gt; log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

\[root@localhost test]# 

说明：

在shell中用任何方式删除文件之前，应当先查看相应的文件，一定要小心！当使用诸如mv或rm命令时，可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。 

实例3：在目录中查找更改时间在n日以前的文件并删除它们，在删除之前先给出提示

命令：

find . -name "\*.log" -mtime +5 -ok rm {} \\;

输出：

\[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log\_link.log -&gt; log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

\[root@localhost test]# find . -name "\*.log" -mtime +5 -ok rm {} \\;

&lt; rm ... ./log\_link.log &gt; ? y

&lt; rm ... ./log2012.log &gt; ? n

\[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

\[root@localhost test]#

说明：

在上面的例子中， find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件，并删除它们，只不过在删除之前先给出提示。 按y键删除文件，按n键不删除。 

 

实例4：-exec中使用grep命令

命令：

find /etc -name "passwd\*" -exec grep "root" {} \\;

输出：

\[root@localhost test]# find /etc -name "passwd\*" -exec grep "root" {} \\;

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

\[root@localhost test]#

说明：

任何形式的命令都可以在-exec选项中使用。  在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd\*”的文件，例如passwd、passwd.old、passwd.bak，然后执行grep命令看看在这些文件中是否存在一个root用户。

 

实例5：查找文件移动到指定目录  

命令：

find . -name "\*.log" -exec mv {} .. \\;

输出：

\[root@localhost test]# ll

总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-12 22:49 test3

drwxrwxr-x 2 root root 4096 11-12 19:32 test4

\[root@localhost test]# cd test3/

\[root@localhost test3]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

\[root@localhost test3]# find . -name "\*.log" -exec mv {} .. \\;

\[root@localhost test3]# ll

总计 0\[root@localhost test3]# cd ..

\[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

\[root@localhost test]#

实例6：用exec选项执行cp命令  

命令：

find . -name "\*.log" -exec cp {} test3 \\;

输出：

\[root@localhost test3]# ll

总计 0\[root@localhost test3]# cd ..

\[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

\[root@localhost test]# find . -name "\*.log" -exec cp {} test3 \\;

cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件

cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件

cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件

\[root@localhost test]# cd test3

\[root@localhost test3]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

\[root@localhost test3]#

 

1\. 想查看当前文件夹及子文件夹里有没有文件名为“abc”的文件

\# find . -name abc

. ：表示当前目录

-name：表示要根据名称查找

2\. 想查看当前文件夹及子文件夹里有没有”xyz”目录

\# find . -type d -name xyz

-type：表示设定类型，d表示文件夹类型，可以替换为f(普通文件)、l(链接文件)

3\. 想找出当前文件夹及子文件夹里所有后缀是”.txt”的文件

\# find . -name “\*.txt”

4\. 想查找当前目录及其子文件夹中“roc”用户自己的文件有哪些

\# find . -user roc

-user：用于设定所属用户的名称，此处可替换为-group，即所属用户组的名称

5\. 想查找当前文件夹及子文件夹里权限设定为755的所有文件

 

\# find . -perm 755

-perm：用于设定权限

6\. 想查找当前文件夹及子文件夹里的同时含有b字符和3字符的文件：用到正则表达式技术

\# find . -regex ‘.\*b.\*3′

-regex：表示使用正则表达式进行匹配。请注意，此命令会和“全路径”进行匹配，也就是说前面要加.\*，因为输出结果中会有“./”符号。

7\. 如果想全部输出用find命令查找出的”\*.abc”文件的内容

\# find . -type f -name “\*.abc” -exec cat {} \\;

-exec 表示由find找到的匹配项会作为“-exec后面设定的命令”的参数

可以使用-ok代替-exec，这样对每个匹配项进行操作，都会要求用户确认（y为是，n为否）

命令最后的{} \\; 别忘了写，其中{}代表用find查找到的结果中的每一个查找项。

8\. 查找当前目录下在5分钟内被访问过的文件

\# find . -amin -5

访问过用amin，修改过用mmin，文件状态改变过用cmin

精确到分钟的用amin,mmin,cmin，精确到天的用atime,mtime,ctime

在5分钟之内的用-5，在5分钟以上的用＋5

9\. 想查找当前目录及子目录下文件大小大于10M的所有文件

\# find . -size +10000000c

-size：表示文件大小，＋表示大于某个数，－表示小于某个数。c表示单位是字节，你可以将c换成k,M,G。

10\. 上述所有的find命令都是查找当前目录及其子目录。如果不想深入到子目录中，而是只查找当前一层目录，则可以：

 

\# find . **-maxdepth 1** -name “\*.c”