find:查找文件

xiaohai 2021-05-05 16:53:55 2061人围观 标签: Linux 
简介这个一个比较常用的命令,需要熟练掌握。find命令主要是在指定的目录下查找文件。如果find后面不使用任何参数,那么就查找当前目录下的所有子文件和目录,全部显示出来。
说明:

  这个一个比较常用的命令,需要熟练掌握。find命令主要是在指定的目录下查找文件。如果find后面不使用任何参数,那么就查找当前目录下的所有子文件和目录,全部显示出来。

格式:

  find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]

参数说明:

  • -amin[分钟]:查找在指定时间曾被存取过的文件或目录,单位以分钟计算;
  • -anewer[参考文件或目录]:查找其存取时间较指定文件或目录的存取时间更接近现在的文件或目录;
  • -atime[24小时数]:查找在指定时间曾被存取过的文件或目录,单位以24小时计算;
  • -cmin[分钟]:查找在指定时间之时被更改过的文件或目录;
  • -cnewer[参考文件或目录]查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;
  • -ctime[24小时数]:查找在指定时间之时被更改的文件或目录,单位以24小时计算;
  • -daystart:从本日开始计算时间;
  • -depth:从指定目录下最深层的子目录开始查找;
  • -delete:将查找出的文件删除;
  • -expty:寻找文件大小为0 Byte的文件,或目录下没有任何子目录或文件的空目录;
  • -exec[执行指令]:假设find指令的回传值为True,就执行该指令;
  • -false:将find指令的回传值皆设为False;
  • -fls[列表文件]:此参数的效果和指定“-ls”参数类似,但会把结果保存为指定的列表文件;
  • -follow:排除符号连接;
  • -fprint[列表文件]:此参数的效果和指定“-print”参数类似,但会把结果保存成指定的列表文件;
  • -fprint0[列表文件]:此参数的效果和指定“-print0”参数类似,但会把结果保存成指定的列表文件;
  • -fprintf[列表文件][输出格式]:此参数的效果和指定“-printf”参数类似,但会把结果保存成指定的列表文件;
  • -fstype[文件系统类型]:只寻找该文件系统类型下的文件或目录;
  • -gid[群组识别码]:查找符合指定之群组识别码的文件或目录;
  • -help或——help:在线帮助;
  • -ilname[范本样式]:此参数的效果和指定“-lname”参数类似,但忽略字符大小写的差别;
  • -iname[范本样式]:此参数的效果和指定“-name”参数类似,但忽略字符大小写的差别;
  • -inum[inode编号]:查找符合指定的inode编号的文件或目录;
  • -ipath[范本样式]:此参数的效果和指定“-path”参数类似,但忽略字符大小写的差别;
  • -iregex[范本样式]:此参数的效果和指定“-regexe”参数类似,但忽略字符大小写的差别;
  • -links[连接数目]:查找符合指定的硬连接数目的文件或目录;
  • -iname[范本样式]:指定字符串作为寻找符号连接的范本样式;
  • -ls:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出;
  • -maxdepth[目录层级]:设置最大目录层级;
  • -mindepth[目录层级]:设置最小目录层级;
  • -mmin[分钟]:查找在指定时间曾被更改过的文件或目录,单位以分钟计算;
  • -mount:此参数的效果和指定“-xdev”相同;
  • -mtime[24小时数]:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
  • -name[范本样式]:指定字符串作为寻找文件或目录的范本样式;
  • -newer[参考文件或目录]:查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;
  • -noleaf:不去考虑目录至少需拥有两个硬连接存在;
  • -ok[执行指令]:此参数的效果和指定“-exec”类似,但在执行指令之前会先询问用户,若回答“y”或“Y”,则放弃执行命令;
  • -path[范本样式]:指定字符串作为寻找目录的范本样式;
  • -perm[权限数值]:查找符合指定的权限数值的文件或目录;
  • -print:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为每列一个名称,每个名称前皆有“./”字符串;
  • -print0:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为全部的名称皆在同一行
  • -printf[输出格式]:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式可以自行指定;
  • -prune:不寻找字符串作为寻找文件或目录的范本样式;
  • -regex[范本样式]:指定字符串作为寻找文件或目录的范本样式
  • -size[文件大小]:查找符合指定的文件大小的文件;
  • -true:将find指令的回传值皆设为True;
  • -type[文件类型]:只寻找符合指定的文件类型的文件;
  • -uid[用户识别码]:查找符合指定的用户识别码的文件或目录;
  • -used[日数]:查找文件或目录被更改之后在指定时间曾被存取过的文件或目录,单位以日计算;
  • -user[拥有者名称]:查找符和指定的拥有者名称的文件或目录;
  • -nouser:找出不属于本地主机用户识别码的文件或目录;
  • -group[群组名称]:查找符合指定之群组名称的文件或目录;
  • -nogroup:找出不属于本地主机群组识别码的文件或目录;
  • -xdev:将范围局限在先行的文件系统中;
  • -xtype[文件类型]:此参数的效果和指定“-type”参数类似,差别在于它针对符号连接检查。

示例:

1、find不指定参数
[root@localhost test]# find
.
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01
./dir01/test2.txt
./dir01/abc.txt
./dir01/abc
./dir01/def
./dir02
./dir03
./dir04
./dir05
./abc1.txt
./abc2.txt


2、通过文件指定的目录查看
[root@localhost test]# find dir01
dir01
dir01/test2.txt
dir01/abc.txt
dir01/abc
dir01/def

3、通过指定目录和名字进行查找[-name参数,可以使用通配符]
[root@localhost test]# find dir01/ -name "abc*"
dir01/abc.txt
dir01/abc
[root@localhost test]# find dir01/ -name "test*"
dir01/test2.txt
[root@localhost test]# find dir01/ -name abc
dir01/abc

4、也可使用正则表达式进行查找
[root@localhost test]# find dir01/ -regex ".*abc.*"
dir01/abc.txt
dir01/abc
注意:前面的.一定不能省略

5、查到当前目录下以.txt结尾的文件
[root@localhost test]# find . -name '*.txt'
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01/test2.txt
./dir01/abc.txt
./abc1.txt
./abc2.txt

6、否定参数[!],查找当前目录下不以.txt结尾的文件
[root@localhost test]# find . ! -name '*.txt'
.
./dir01
./dir01/abc
./dir01/def
./dir02
./dir03
./dir04
./dir05

7、根据文件的类型进行搜索[-type]
类型参数列表:
    f 普通文件
    l 符号连接
    d 目录
    c 字符设备
    b 块设备
    s 套接字
    p Fifo

[root@localhost test]# find . -type d
.
./dir01
./dir01/abc
./dir01/def
./dir02
./dir03
./dir04
./dir05
[root@localhost test]# find . -type f
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01/abc.txt
[root@localhost test]# find . -type l
./dir01/test2.txt
./abc1.txt
./abc2.txt
[root@localhost test]# find . -type c
[root@localhost test]# find . -type b
[root@localhost test]# find . -type s
[root@localhost test]# find . -type p


8、查找最大深度[-maxdepth]
[root@localhost test]# find . -maxdepth 2 -name '*.txt'
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01/test2.txt
./dir01/abc.txt
./abc1.txt
./abc2.txt
[root@localhost test]# find . -maxdepth 1 -name '*.txt' #当前目录下不包含子目录的文件
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./abc1.txt
./abc2.txt

9、查找最小深度[-mindepth]
[root@localhost test]# find . -mindepth 1 -name '*.txt'
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01/test2.txt
./dir01/abc.txt
./abc1.txt
./abc2.txt
[root@localhost test]# find . -mindepth 2 -name '*.txt'
./dir01/test2.txt
./dir01/abc.txt

10、根据时间进行查找[-atime,-mtime,-ctime]
[root@localhost test]# find . -type f -atime +1 -name "*.txt" #1天以前的
[root@localhost test]# find . -type f -atime -1 -name "*.txt" #1天以内的
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01/abc.txt
[root@localhost test]# find . -type f -atime +2 -name "*.txt" #2天以前的
[root@localhost test]# find . -type f -atime 2 -name "*.txt" #2天前的
[root@localhost test]# find . -type f -atime 1 -name "*.txt" #1天前的

说明:
+N:距当前时间N天以前的
-N:距当前时间N天以内的
N:据当前时间N天的

11、根据大小进行查询[-size]
[root@localhost test]# find . -type f -size +1c #c字节单位
./test2.txt
./test5.txt

12、查找文件时希望忽略某个目录
[root@localhost test]# find . -path "./dir01" -prune -o -print
.
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir02
./dir03
./dir04
./dir05
./abc1.txt
./abc2.txt


13、使用user和nouser查找文件
[root@localhost test]# chown nginx.nginx test2.txt 
[root@localhost test]# ll
总用量 8
lrwxrwxrwx. 1 root  root   9 5月   3 07:30 abc1.txt -> test5.txt
lrwxrwxrwx. 1 root  root  17 5月   3 08:46 abc2.txt -> ../test/test2.txt
drwxr-xr-x. 4 root  root  56 5月   3 07:36 dir01
drwxr-xr-x. 2 root  root   6 5月   3 07:00 dir02
drwxr-xr-x. 2 root  root   6 5月   3 07:00 dir03
drwxr-xr-x. 2 root  root   6 5月   3 07:00 dir04
drwxr-xr-x. 2 root  root   6 5月   3 07:00 dir05
-rw-r--r--. 1 nginx nginx 27 5月   3 15:31 test2.txt
-rw-r--r--. 1 root  root   0 5月   3 07:00 test3.txt
-rw-r--r--. 1 root  root   0 5月   3 07:00 test4.txt
-rw-r--r--. 1 root  root   2 5月   3 08:42 test5.txt
[root@localhost test]# find . -user nginx
./test2.txt
[root@localhost test]# chown 555 test3.txt 
[root@localhost test]# find . -nouser
./test3.txt

14、查找比某个文件新或旧文件[-newer]
[root@localhost test]# find . -newer test5.txt 
.
./test2.txt
./abc2.txt

15、-exec选择的使用
[root@localhost test]# find . -type f -exec ls {} \; #注意这里必须使用\; 否则会报错 {}代表find查找到的内容
./test2.txt
./test3.txt
./test4.txt
./test5.txt
./dir01/abc.txt

16、使用-exec的安全模式-ok
[root@localhost test]# find . -type f -ok rm {} \;
< rm ... ./test2.txt > ? n
< rm ... ./test3.txt > ? n
< rm ... ./test4.txt > ? n
< rm ... ./test5.txt > ? n
< rm ... ./dir01/abc.txt > ? y


17、配合xargs使用
[root@localhost test]# find . -type f|xargs ls -l
-rw-r--r--. 1 nginx nginx 27 5月   3 15:31 ./test2.txt
-rw-r--r--. 1   555 root   0 5月   3 07:00 ./test3.txt
-rw-r--r--. 1 root  root   0 5月   3 07:00 ./test4.txt
-rw-r--r--. 1 root  root   2 5月   3 08:42 ./test5.txt
[root@localhost test]# find . -type f -name "*4*"|xargs rm #删除匹配到4的文件


18、exce和xargs处理特殊名称时的区别
-exce可以正常处理
xargs则需要通过print0和xargs -0来进行处理
[root@localhost test]# find . -name "a*b" -exec ls -l {} \;
-rw-r--r--. 1 root root 0 5月   3 16:57 ./a b
[root@localhost test]# find . -name "a*b" -print0|xargs -0 ls -l
-rw-r--r--. 1 root root 0 5月   3 16:57 ./a b