find是一个非常强大的搜索命令,由此,参数等也很多,学习起来需要慢慢的记忆理解。
与locate查索引库不同,find是遍历硬盘上的文件,所以是非常消耗资源的。
命令格式
find [路径] [参数] [-print -exec -ok...]
这里注意与locate的区别,locate无需指定目录
locate [参数]
下面从最简单的开始。
示例1、在当前目录下根据名称查找
.代表当前目录;
-name 查找文件/目录的名字
下面还可以看出,不带. 默认仍为当前目录
[root@localhost a]# pwd
/home/syq/a
[root@localhost a]# find . -name 1.txt
./ttt/1.txt
[root@localhost a]# find -name 1.txt
./ttt/1.txt
示例2、在指定目录下根据名称查找
[root@localhost /]# find -name 1.txt
find: ‘./run/user/1000/gvfs’: Permission denied
./home/syq/Pictures/1.txt
./home/syq/a/ttt/1.txt
[root@localhost /]# find /home/syq/Pictures/ -name 1.txt
/home/syq/Pictures/1.txt
示例3、在指定目录下查找.sh后缀的文件
.sh代表以sh为后缀
仔细看下面,直接这样查找时返回了一个错误;
这是因为被当做shell,需进行转义,使用*即可。或者加引号"*.sh"
[root@localhost ttt]# ll
total 0
-rwxrwxrwx. 1 root root 0 Mar 29 14:59 1.sh
-rw-r--r--. 1 root root 0 Mar 29 14:41 1.txt
-rwxrwxrwx. 1 root root 0 Mar 29 14:59 2.sh
-rw-r--r--. 1 root root 0 Mar 29 14:41 2.txt
-rwxrwxrwx. 1 root root 0 Mar 29 14:59 3.sh
-rw-r--r--. 1 root root 0 Mar 29 14:41 3.txt
[root@localhost ttt]# find . -name *.sh
find: paths must precede expression: 2.sh
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@localhost ttt]# find . -name \*.sh
./1.sh
./2.sh
./3.sh
示例4、查找指定类型的
-f
一般文件
[root@localhost ttt]# ls
1.sh 1.txt 2.sh 2.txt 3.sh 3.txt file1 file2 file3 z1 z2 z3
[root@localhost ttt]# find . -type f
./1.txt
./2.txt
./3.txt
./1.sh
./2.sh
./3.sh
./file1
./file2
./file3
[root@localhost ttt]#
-d
目录
[root@localhost ttt]# find . -type d
.
./z1
./z2
./z3
-b
块设备 block
[root@localhost dev]# find -type b
./dm-1
./dm-0
./sr0
./nvme0n1p2
./nvme0n1p1
./nvme0n1
-s
套接字
-p
管道文件
-l
符号链接文件
-c
字符设备文件
示例5、amin/atime cmin/ctime mmin/mtime含义与区别
首先弄清楚a c m 的含义,这里提供一个小技巧,就是man命令,命令的官方解释,这样使用 man find
查到a:accessed,访问过
c:changed,文件状态修改,比如权限等
m:modified,文件数据修改
find [pathname] -amin -10
:
查找系统中最后10分钟内访问的文件
find [pathname] -cmin -10
:
查找系统中最后10分钟内被改变状态的文件
find [pathname] -mmin -10
:
查找系统中最后10分钟内被改变数据的文件
如下是改变文件状态的查询示例
[root@localhost ttt]# chmod 755 1.sh
[root@localhost a]# find . -amin -10
[root@localhost a]# find . -mmin -10
[root@localhost a]# find . -cmin -10
./ttt/1.sh
atime同理
示例6、根据权限查找文件
[root@localhost ttt]# ll
total 4
-rwxr-xr-x. 1 root root 0 Mar 29 14:59 1.sh
-rwxrwxrwx. 1 root root 0 Mar 29 14:59 2.sh
-rwxrwxrwx. 1 root root 0 Mar 29 14:59 3.sh
drwxr-xr-x. 2 root root 6 Mar 29 15:12 z1
drwxr-xr-x. 2 root root 6 Mar 29 15:12 z2
drwxr-xr-x. 2 root root 6 Mar 29 15:12 z3
[root@localhost ttt]# find . -perm 777
./2.sh
./3.sh
[root@localhost ttt]# find . -perm 755
.
./1.sh
./z1
./z2
./z3
[root@localhost ttt]#
示例7、查找目录下的文件并排序
[root@localhost ttt]# find . -type f
./2.txt
./3.txt
./1.sh
./2.sh
./3.sh
./file1
./file2
./file3
./1.txt
[root@localhost ttt]# find . -type f|sort
./1.sh
./1.txt
./2.sh
./2.txt
./3.sh
./3.txt
./file1
./file2
./file3
[root@localhost ttt]#
示例8、按大小查找
-size
带c表示字节
+表示大于 -表示小于 不带表示等于
除此之外,还可以使用or and not 或与非,分别是-o -a !表示
[root@localhost ttt]# find . -size 4c
./1.txt
[root@localhost ttt]# find . -size 3c
[root@localhost ttt]# find . -size 6c
./z1
./z2
./z3