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>

Linux学习总结(11)——Linux文件查找的更多相关文章

  1. linux学习-环境变量与文件查找

  2. <实训|第十一天>学习一下linux中的进程,文件查找,文件压缩与IO重定向

    [root@localhost~]#序言 在今后的工作中,运维工程师每天的例行事务就是使用free -m,top,uptime,df -h...每天都要检查一下服务器,看看是否出现异常.那么今天我们就 ...

  3. Linux学习02--Linux一切皆文件

    Linux学习第二部 Linux一切皆对象 啊啊啊啊啊,今天被学妹说太直了,嘤嘤嘤. 学习linux两三天了,前期感觉并不难,只是命令多,多记记多敲一敲就能都记住了.希望自己能够坚持下去吧! 下面是根 ...

  4. Linux学习心得之 Linux下ant安装与使用

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下ant安装与使用 1. 前言2. ant安装3. 简单的a ...

  5. Linux学习之二-Linux系统的目录结构

    Linux学习之二-Linux系统的目录结构 在Linux的根目录下,有很多的目录,但是需要记住,对于Linux而言,一切皆文件.因此此处的目录也是文件.用ls / 命令就能看到根目录下的各类不同的目 ...

  6. Linux学习心得之 Linux下命令行Android开发环境的搭建

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...

  7. linux学习书籍推荐linux学习书籍推荐

    引用地址:http://www.cnblogs.com/notepi/archive/2013/06/15/3137103.html Linux 学习书目推荐 Linux基础 1.<Linux与 ...

  8. Linux的VMWare中Centos7文件查找(find-grep)和vim文本编辑器基操

    一.find文件查找 grep 匹配字段 文件名 ——筛选文件   find查找 语法参数示例 格式: find 查找范围 查找类型 参数   find / -name *.conf   按文件名查找 ...

  9. Linux内核0.11 makefile文件说明

    # # if you want the ram-disk device, define this to be the # size in blocks. # 如果要使用 RAM 就定义块的大小(注释掉 ...

  10. Linux使用vim进行多文件查找和替换的方法

    vim是Linux系统下常用的文本编辑,通过使用多种shell命令能够实现多文件的查找和替换,那么具体会使用到那些命令呢?下面小编就给大家介绍下Linux系统vim多文件查找和替换的方法. 在linu ...

随机推荐

  1. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)A. Protect Sheep

    http://codeforces.com/contest/948/problem/A   A. Protect Sheep Bob is a farmer. He has a large pastu ...

  2. [Atcoder Code Festival 2017 Qual B Problem F]Largest Smallest Cyclic Shift

    题目大意:给你\(A\)个a,\(B\)个b,\(C\)个c,要你构造一个字符串,使它的最小循环表示法最大.求这个表示法.解题思路:不知道怎么证,但把a.b.c当做单独的字符串扔进容器,每次把字典序最 ...

  3. 【转】python 关键字

    转自:http://www.cnblogs.com/hongten/p/hongten_python_keywords.html python3.3.2中的关键字如下: The following i ...

  4. js兼容性——获取当前浏览器窗口的宽高

    通过onresize事件 window.onresize = function () { document.title = client().width + " "+ client ...

  5. ThinkPHP 跨模块调用操作方法(A方法与R方法)

    ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

  6. WinServer-IIS-FTP服务器

    在设置FTP服务器的时候,注意SSL的设置,简单尝试的话,不需要启用SSL 常用FTP命令 ftp   进入FTP命令模式 open 192.168.10.6     (连接到FTP主机,或者open ...

  7. 洛谷 P3133 [USACO16JAN]无线电联系Radio Contact

    P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the ...

  8. 极路由4pro(HC5962)安装python

    基本配置 其实极路由4.极路由4 pro.极路由B70都是一个型号的(HC5962) CPU:MT7621AT + MT7612EN + 7603EN 内存:256MB DDR3 RAM 闪存:128 ...

  9. java 日期和字符串互转,依据当天整天时间 得到当天最后一秒的日期时间

    java 日期和字符串互转.依据当天整天时间   得到当天最后一秒的日期时间 package com.hi; import java.text.DateFormat; import java.text ...

  10. 《Objective-C高级编程:iOS与OS X多线程和内存管理》读后感

    拿到这本书的第一感觉是非常薄,可是内容就如同序里面所说,这不是一本面向刚開始学习的人的书,比較有深度,对C/C++全然不熟悉的话非常多东西会看不明确. 尽管此书在技术点上仅仅谈到了ARC.Blocks ...