Linux下的常用查找命令

  • locate
  • whereis
  • which
  • find

locate 

-i, 忽略大小写 

find 

根据文件名或正则表达式搜索 

-name 

 

条件限制 

-a 与条件 

-o 或条件 

-not 非条件(此参数的功能类似于否定参数‘!’)

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"t*"</span> <span class="hljs-attribute" style="box-sizing: border-box;">-a</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span>  <span class="hljs-comment" style="box-sizing: border-box;">//查找以t开头并且以.text结尾的文件</span>
find <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"t*"</span> <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找以t开头或以.text结尾的文件</span>
find <span class="hljs-attribute" style="box-sizing: border-box;">-not</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"t*"</span> <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找不以t开头或以.text结尾的文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul>

 

设定find命令在子目录中遍历的深度参数: 

-maxdepth 

-mindepth 

根据文件类型搜索:

类型 符号
普通文件 f
符号文件 l
目录 d
字符设备 c
块设备 b
套接字 s
管道文件 p
<code class="hljs fsharp has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">l</span>  //查找此目录及子目录以下的所有符号文件</span>
find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> //普通文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul>

根据文件时间进行查找: 

linux文件系统中的每个文件都有三种时间戳: 

1. 访问时间(atime——access time):就是上次访问这个文件的时间。 

2. 改变时间(ctime——change time):就是文件的inode改变的时间(什么是inode?)当你往一个文件中添加数据或者删除数据的时候,修改文件所有者的时候,链接改变的时候,文件的ctime就会发生改变。 

3. 修改时间(mtime——modification time):就是文件的内容上一次发生改变的时候的时间。

<code class="hljs fsharp has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> -<span class="hljs-title" style="box-sizing: border-box;">atime</span> -7   #查找7天内被访问的文件</span>
find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> -<span class="hljs-title" style="box-sizing: border-box;">atime</span> 7 #查找恰好在7天前被访问的文件</span>
find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> -<span class="hljs-title" style="box-sizing: border-box;">atime</span> +7 #查找超过7天没被访问的文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul>

-atime,-ctime,-mtime可作为find的时间参数,单位是天。还有基于“分钟”的,-amin,-mmin 

-cmin。这些值通常还带有+或-:+表示大于,-表示小于。 

-newer参数,我们可以指定一个用于比较时间戳的参考文件,然后找出比参考文件更新的所有文件。 

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-newer</span> test4  <span class="hljs-comment" style="box-sizing: border-box;">//查找当前目录及子目录下比test4更新的文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li></ul>

基于文件大小的查找 

-size参数 

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-size</span> <span class="hljs-subst" style="box-sizing: border-box;">+</span><span class="hljs-number" style="box-sizing: border-box;">45</span>  <span class="hljs-comment" style="box-sizing: border-box;">//查找文件大小比45大的普通文件</span>
find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-size</span> <span class="hljs-number" style="box-sizing: border-box;">45</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找文件大小为45的普通文件</span>
find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-size</span> <span class="hljs-subst" style="box-sizing: border-box;">-</span><span class="hljs-number" style="box-sizing: border-box;">45</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找文件大小比45小的普通文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul>

基于文件权限和文件所有人的查找 

-perm 

-user 

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-perm</span> <span class="hljs-number" style="box-sizing: border-box;">664</span>   <span class="hljs-comment" style="box-sizing: border-box;">//查找权限为664的普通文件</span>
find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-user</span> lee <span class="hljs-comment" style="box-sizing: border-box;">//查找所有人为lee的普通文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul>

结合find执行命令或动作 

-exec

<code class="hljs mel has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -type f -name <span class="hljs-string" style="box-sizing: border-box;">"*.c"</span> -<span class="hljs-keyword" style="box-sizing: border-box;">exec</span> cat> <span class="hljs-keyword" style="box-sizing: border-box;">file</span>.txt \;  #将所有.c文件拼接起来写入单个文件<span class="hljs-keyword" style="box-sizing: border-box;">file</span>.txt中
find -type f -mtime +<span class="hljs-number" style="box-sizing: border-box;">10</span> -name <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> -<span class="hljs-keyword" style="box-sizing: border-box;">exec</span> cp /<span class="hljs-number" style="box-sizing: border-box;">5</span>_15 #将<span class="hljs-number" style="box-sizing: border-box;">10</span>天前的.<span class="hljs-keyword" style="box-sizing: border-box;">text</span>文件复制到/tmp下</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul>

find与xargs结合使用 

xargs参数 将标准输入转换成命令行参数 

<code class="hljs bash has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -type f -name <span class="hljs-string" style="box-sizing: border-box;">"*.txt"</span> -print0 |xargs  -<span class="hljs-number" style="box-sizing: border-box;">0</span> rm <span class="hljs-operator" style="box-sizing: border-box;">-f</span>   <span class="hljs-comment" style="box-sizing: border-box;">#将匹配到的文件删除,xargs -0将 \0作为输入定界符。</span>
find -type f -name <span class="hljs-string" style="box-sizing: border-box;">"*.c"</span> -print0 | xargs -<span class="hljs-number" style="box-sizing: border-box;">0</span> <span class="hljs-operator" style="box-sizing: border-box;">-l</span> <span class="hljs-comment" style="box-sizing: border-box;">#统计所有c程序文件的行数</span></span></code>

最新文章

  1. ASP.NET:在一般处理程序中通过 Session 保存验证码却无法显示图片?
  2. Redhat6.5使用centos yum源
  3. CSS3定位和浮动详解
  4. sql查看数据字典(表结构)
  5. 类模板的困扰 LNK2019 (转)
  6. React编写文本评论框
  7. mac OS X下git代码行统计命令
  8. DP练习(初级):ZigZag
  9. 22. Generate Parentheses
  10. 【lucene系列学习二】Lucene实现高亮显示关键词
  11. Kotlin——最详细的接口使用、介绍
  12. MySQL语句高效写法整理
  13. js判断是否下拉刷新
  14. ILMerge参考文档
  15. LINUX第三次实践:程序破解
  16. Ubuntu16.04安装xgboost
  17. IntelliJ IDEA:Field injection is not recommended
  18. DELPHI XE Android 开发笔记
  19. Java中String直接复制和new String创建对象的区别以及equals和==的区别和效率对比
  20. 网络编程基础——学习阻塞,非阻塞(select和epoll)

热门文章

  1. HDU 1757 A Simple Math Problem( 矩阵快速幂 )
  2. C文件I/O超详细教程
  3. unity 支持圆形、切倒角和虚化UGUI Shader
  4. 在join中,on和where的区别
  5. onkeydown、onkeypress、onkeyup、onblur、onchange、oninput、onpropertychange的区别
  6. 将Oracle中的数据放入elasticsearch
  7. SQL SERVER-union
  8. Chrome插件Axure RP Extension
  9. MarkDown、Vim双剑合璧
  10. UESTC 1143 数据传输 网络流 最大流 Dinic