find命令用于搜索文件或者目录,find命令的参数特别多,我们只列举常用的:
| 参数 | 解释 |
|---|---|
| -name | 以文件或者目录名称搜索 |
| -user | 以文件所属用户搜索 |
| -type | 限制文件类型,f为文件,d为文件夹 |
| -maxdepth | 最大搜索深度 |
[root@linuxforliuhj test]# ll total 0 -rw-r--r--. 1 root root 0 Nov 3 00:10 Alis.txt -rw-r--r--. 1 root root 0 Nov 3 00:10 Bouch.sh -rw-r--r--. 1 root root 0 Nov 3 00:10 Count.jpg -rw-r--r--. 1 hadoop101 hadoop101 0 Nov 3 00:10 Zero -rw-r--r--. 1 hadoop101 hadoop101 0 Nov 3 00:09 alis.txt -rw-r--r--. 1 root root 0 Nov 3 00:10 bouch.sh -rw-r--r--. 1 root root 0 Nov 3 00:10 count.jpg drwxr-xr-x. 2 root root 6 Nov 3 00:39 dir1 drwxr-xr-x. 2 root root 6 Nov 3 00:39 dir2 -rw-r--r--. 1 root root 0 Nov 3 00:10 zero [root@linuxforliuhj test]#
命令find /test/ -maxdepth 3 -type f -name "*i*"
/test/ :表示在/test/目录下搜索
-maxdepth 3 : 最大搜索深度为3
-type f : 搜索类型为文件file
-name : 以文件名搜索,后面则是通配符
[root@linuxforliuhj test]# find /test/ -maxdepth 3 -type f -name "*i*" /test/alis.txt /test/Alis.txt [root@linuxforliuhj test]#
命令find /test/ -user hadoop101
/test/ :表示在/test/目录下搜索
-user : 搜索文件属主为hadoop101用户的文件
[root@linuxforliuhj test]# find /test/ -maxdepth 3 -type f -user hadoop101 /test/alis.txt /test/Zero [root@linuxforliuhj test]#
注意
maxdepth参数尽量放在type、name/user参数之前,例如执行find /test/ -maxdepth 3 -type f -name "*i*"不会有任何报错,但是执行find /test/ -type f -maxdepth 3 -name "*i*"会有warning提示。
[root@linuxforliuhj test]# find /test/ -type f -maxdepth 3 -name "*i*" find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. /test/alis.txt /test/Alis.txt



