linux常用命令(19):find命令概览

find命令

        find命令用来在目录结构中搜索文件,并执行指定的操作Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只要你具有相应的权限。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。


语法

find (选项)(参数)    ||   find pathname -options [-print -exec -ok ...]
------------------------------------------------------------------------------------
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。 
-print: find命令将匹配的文件输出到标准输出。 
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {  } \;,注意{   }和\;之间的空格。 
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

选项

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

参数

起始目录:查找文件的起始目录。

常用范例

实例一:查找指定时间内修改过的文件 

命令:

           find -atime -2

输出:

[root@peidachang ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

说明:

超找48小时内修改过的文件 

实例二:根据关键字查找 

命令:

find . -name "*.log"

输出:

[root@localhost test]# find . -name "*.log" 

./log_link.log

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

说明:

在当前目录查找 以.log结尾的文件。 ""代表当前目录 

实例三:按照目录或文件的权限来查找文件

命令:

find /opt/soft/test/ -perm 777

输出:

[root@localhost test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

说明: 

查找/opt/soft/test/目录下 权限为 777的文件

实例四:按类型查找 

命令:

find . -type f -name "*.log"

输出:

[root@localhost test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

说明:

查找当目录,以.log结尾的普通文件 

实例五:查找当前所有目录并排序

命令:

find . -type d | sort

输出:

[root@localhost test]# find . -type d | sort

.

./scf

./scf/bin

./scf/doc

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/info

./scf/service/deploy/product

./test3

./test4

./test5

./test5/test3

[root@localhost test]#

实例六:按大小查找文件

命令:

find . -size +1000c -print

输出:

[root@localhost test]#  find . -size +1000c -print

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./log2012.log

./test5

./test5/test3

./test3

[root@localhost test]#

说明:

    查找当前目录大于1K的文件 


根据文件或者正则表达式进行匹配

      列出当前目录及子目录下所有文件和文件夹 

find .
      

      在/home目录下查找以.txt结尾的文件名  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find /home -name "*.txt"</span>

同上,但忽略大小写  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find /home -iname "*.txt"</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"> </span>


当前目录及子目录下查找所有以.txt和.pdf结尾的文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . \( -name "*.txt" -o -name "*.pdf" \)  或  find . -name "*.txt" -o -name "*.pdf"</span>


匹配文件路径或者文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find /usr/ -path "*local*"</span>


基于正则表达式匹配文件路径  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -regex ".*\(\.txt\|\.pdf\)$"</span>


同上,但忽略大小写  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;">find . -iregex ".*\(\.txt\|\.pdf\)$"</span>



否定参数

     找出/home下不是以.txt结尾的文件 

<span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; font-size: 14px; line-height: 18px; text-indent: 28px;">find /home ! -name "*.txt"</span>
      

根据文件类型进行搜索

类型参数列表:  f 普通文件 

                           l 符号连接 

                           d 目录 

                           c 字符设备 

                           b 块设备 

                           s 套接字 p Fifo 

find . -type 类型参数
      

基于目录深度搜索

      向下最大深度限制为3  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -maxdepth 3 -type f</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"> </span>
      

      搜索出深度距离当前目录至少2个子目录的所有文件 

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"> find . -mindepth 2 -type f</span></span>


根据文件时间戳进行搜索

find . -type f 时间戳
        UNIX/Linux文件系统每个文件都有三种时间戳: 

     访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。 

     修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间。 

     变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。


     搜索最近七天内被访问过的所有文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -atime -7</span></span>

搜索恰好在七天前被访问过的所有文件 

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"></span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"> find . -type f -atime 7</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"></span>


搜索超过七天内被访问过的所有文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -atime +7</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"> </span></span>


搜索访问时间超过10分钟的所有文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -amin +10</span></span>


找出比file.log修改时间更长的所有文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -newer file.log</span></span>



根据文件大小进行匹配

find . -type f -size 文件大小单元
     文件大小单元: 
          b —— 块(512字节) 
          c —— 字节 
          w —— 字(2字节) 
 k —— 千字节 
  M —— 兆字节 
 G —— 吉字节

     搜索大于10KB的文件    

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="font-family: 'Times New Roman'; font-size: 14px; text-indent: 28px;">find . -type f -size +10k</span></span>

   搜索小于10KB的文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -size -10k</span></span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"> </span>

搜索等于10KB的文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -size 10k</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">  </span></span>



删除匹配文件

  删除当前目录下所有.txt文件  

<span style="font-family: 'Times New Roman'; font-size: 14px; text-indent: 28px;">find . -type f -name "*.txt" -delete</span>
      


根据文件权限/所以权进行匹配

      当前目录下搜索出权限为777的文件  

<span style="font-family: 'Times New Roman'; font-size: 14px; text-indent: 28px;">find . -type f -perm 777</span>
      

     找出当前目录下权限不是644的php文件   

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="font-family: 'Times New Roman'; font-size: 14px; text-indent: 28px;">find . -type f -name "*.php" ! -perm 644</span><span style="font-family: 'Times New Roman'; font-size: 14px; text-indent: 28px;"> </span></span>

     找出当前目录用户tom拥有的所有文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"></span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -user tom</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"></span>


     找出当前目录用户组sunk拥有的所有文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -type f -group sunk</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">  </span></span>


借助-exec选项与其他命令结合使用

     找出当前目录下所有root的文件,并把所有权更改为用户tom   

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find .-type f -user root -exec chown tom {} \;</span>
              上例中,{} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。

     找出自己家目录下所有的.txt文件并删除

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find $HOME/. -name "*.txt" -ok rm {} \;</span>
                   上例中,-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。

     

查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;"><span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; font-size: 14px; line-height: 18px; text-indent: 28px;">find . -type f -name "*.txt" -exec cat {} \;> all.txt</span></span>
                 

     将30天前的.log文件移动到old目录中  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;"><span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; font-size: 14px; line-height: 18px; text-indent: 28px;">find . -type f -mtime +30 -name "*.log" -exec cp {} old \;</span></span>
                   

     找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来    

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;"><span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; line-height: 18px;"><span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; font-size: 14px; line-height: 18px; text-indent: 28px;">find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;</span></span></span>
                   

     因为单行命令中-exec参数中无法使用多个命令,以下方法可以实现在-exec之后接受多条命令  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;"><span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; line-height: 18px;"><span style="color: rgb(45, 55, 75); font-family: Verdana, Geneva, sans-serif; font-size: 14px; line-height: 18px; text-indent: 28px;">-exec ./text.sh {} \;</span></span></span>
                    

搜索但跳出指定的目录

       查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;">find . -path "./sk" -prune -o -name "*.txt" -print</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; line-height: 24px;"> </span>

    

find其他技巧收集

       要列出所有长度为零的文件  

<span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"></span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;">find . -empty</span><span style="color: rgb(51, 51, 51); font-family: 'Times New Roman'; font-size: 14px; line-height: 24px; text-indent: 28px;"></span>


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值