1.What is find?

          在linux当中用于文件搜索的命令主要用两个,一个是locate,另一个就是find。我们平常使用最多就是find命令,现在我们就来一步步学习find命令,刚刚接触到find命令的同学可能会觉得这个命令有些难度,不错,相比其他的一些常规命令,这个命令学习起来确实有一点点难度,主要是因为这个命令的选项和参数比较多。


2.How to use find?

          find命令的选项和参数确实比较多,所以刚刚接触的时候不要试图全部掌握,先学会一些简单使用规则,然后在实际应用中使用到它,然后再去学习更高端的规则,进而学习更多的参数和选项。

          我们先man一下find

1
2
3
4
5
NAME
find- search forfiles ina directory hierarchy
SYNOPSIS
find[-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

          看看这个手册就觉得这个命令好复杂,不过我们先忘掉它的复杂,从简单的开始

1
2
[root@localhost ~]# find aa -name file1 -print
aa/file1

          我们先来解释一下这个命令的执行过程:在aa目录里搜索名字是file1的文件,搜索到之后将之打印出来

          看明白这个执行过程之后就简单多了

          我们看看find命令的格式:


       find + [path] + [option] + [test] + [action]

            path:这个不用解释,就是我们想要搜索的路径


            option:这个选项是对前面的路径进行修饰的,不如常见的选项有:

                     -maxdepth N 搜索N层目录

                     -mount 不搜索其他文件系统


            test:这是学习find命令的重点,可以根据文件的各种属性来做条件判断,比如文件名,修改时间,文件的属组等等,更多的判断条件请看手册,这里的条件测试可以多个条件测试组合测试,比如文件名是aaa,而且是目录,就可以是:-name aaa -and -type d。如果多个条件测试的时候可以就是括号以确定优先级,值得注意的是,括号()在bash中是特殊字符,所以需要加\转义。


            action:这是find命令的收尾工作,当文件与我们前面指定的各种匹配测试通过之后,就说明该文件就是正是我们要寻找的文件,那么我们需要对这些文件做什么样的处理工作,就交给这个action来完成,我们列举最常用的action:

                       -print :打印,就是将搜索到的文件打印在屏幕上

                       -exec :执行一些命令,也就是说这个命令后面跟随一些命令,指定使用 \;结束,魔术字符串{}时-exec的一个特殊参数,表示前面搜索到的文件的完全路径,比如要用ls -l命令来显示搜索岛的文件:find [path] [test] -exec ls -l {} \;


3.Examples

     不能光说不练,我们来举几个例子

       先来看看我aa目录下的所有目录和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~]# find aa
aa
aa/b3333
aa/b3333/cc22
aa/b3333/ccc11
aa/b3333/ccc33
aa/file3
aa/b111
aa/b111/cc22
aa/b111/ccc11
aa/b111/ccc33
aa/b222
aa/b222/cc22
aa/b222/ccc11
aa/b222/ccc33
aa/file1
aa/file2
aa/_file
[root@localhost ~]#

            再来看看这里面普通文件

1
2
3
4
5
6
[root@localhost ~]# find aa -type f
aa/file3
aa/file1
aa/file2
aa/_file
[root@localhost ~]#

            我们新建两个文件test,_test,那么我们现在来查找以下划线开头的文件

1
2
3
4
[root@localhost ~]# find aa -type f -and -name '_*'
aa/_test
aa/_file
[root@localhost ~]#

            由于_file文件比test文件先创建,_test文件比test文件后创建,所以我们一test文件做参考,有下划线开头,而且比test后创建的文件

1
2
3
[root@localhost ~]# find aa \( -type f -and -name '_*' -and -newer aa/test \)
aa/_test
[root@localhost ~]#

            看看上面的这条命令,有测试条件太多,所以我用了括号包起来,这样直观一些,括号是特殊字符,记得转义哦。

            我们来显示所以目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# find aa -type d
aa
aa/b3333
aa/b3333/cc22
aa/b3333/ccc11
aa/b3333/ccc33
aa/b111
aa/b111/cc22
aa/b111/ccc11
aa/b111/ccc33
aa/b222
aa/b222/cc22
aa/b222/ccc11
aa/b222/ccc33
[root@localhost ~]#

           我们现在来尝试使用最后的action,上面显示的这么多目录,我们用ls -l来显示它们的详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@localhost ~]# find aa -type d -exec ls -l {} \;
total 12
-rw-r--r--. 1 root root    0 Feb 24 19:31 _file
-rw-r--r--. 1 root root    0 Feb 24 20:23 _test
drwxr-xr-x. 5 root root 4096 Feb 24 19:28 b111
drwxr-xr-x. 5 root root 4096 Feb 24 19:28 b222
drwxr-xr-x. 5 root root 4096 Feb 24 19:28 b3333
-rw-r--r--. 1 root root    0 Feb 24 19:30 file1
-rw-r--r--. 1 root root    0 Feb 24 19:31 file2
-rw-r--r--. 1 root root    0 Feb 24 19:31 file3
-rw-r--r--. 1 root root    0 Feb 24 20:23 test
total 12
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 cc22
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 ccc11
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 ccc33
total 0
total 0
total 0
total 12
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 cc22
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 ccc11
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 ccc33
total 0
total 0
total 0
total 12
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 cc22
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 ccc11
drwxr-xr-x. 2 root root 4096 Feb 24 19:28 ccc33
total 0
total 0
total 0
[root@localhost ~]#

最后

           我也是还是初学者,刚刚接触find这个命令,还在消化之中,以后慢慢补充,欢迎大神们指教^.^