Linux学习总结(11)——Linux文件查找
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文件查找的更多相关文章
- linux学习-环境变量与文件查找
- <实训|第十一天>学习一下linux中的进程,文件查找,文件压缩与IO重定向
[root@localhost~]#序言 在今后的工作中,运维工程师每天的例行事务就是使用free -m,top,uptime,df -h...每天都要检查一下服务器,看看是否出现异常.那么今天我们就 ...
- Linux学习02--Linux一切皆文件
Linux学习第二部 Linux一切皆对象 啊啊啊啊啊,今天被学妹说太直了,嘤嘤嘤. 学习linux两三天了,前期感觉并不难,只是命令多,多记记多敲一敲就能都记住了.希望自己能够坚持下去吧! 下面是根 ...
- Linux学习心得之 Linux下ant安装与使用
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下ant安装与使用 1. 前言2. ant安装3. 简单的a ...
- Linux学习之二-Linux系统的目录结构
Linux学习之二-Linux系统的目录结构 在Linux的根目录下,有很多的目录,但是需要记住,对于Linux而言,一切皆文件.因此此处的目录也是文件.用ls / 命令就能看到根目录下的各类不同的目 ...
- Linux学习心得之 Linux下命令行Android开发环境的搭建
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...
- linux学习书籍推荐linux学习书籍推荐
引用地址:http://www.cnblogs.com/notepi/archive/2013/06/15/3137103.html Linux 学习书目推荐 Linux基础 1.<Linux与 ...
- Linux的VMWare中Centos7文件查找(find-grep)和vim文本编辑器基操
一.find文件查找 grep 匹配字段 文件名 ——筛选文件 find查找 语法参数示例 格式: find 查找范围 查找类型 参数 find / -name *.conf 按文件名查找 ...
- Linux内核0.11 makefile文件说明
# # if you want the ram-disk device, define this to be the # size in blocks. # 如果要使用 RAM 就定义块的大小(注释掉 ...
- Linux使用vim进行多文件查找和替换的方法
vim是Linux系统下常用的文本编辑,通过使用多种shell命令能够实现多文件的查找和替换,那么具体会使用到那些命令呢?下面小编就给大家介绍下Linux系统vim多文件查找和替换的方法. 在linu ...
随机推荐
- debian 9 安装Virtual Box
1.去官网下载deb包,例如包名: virtualbox-.2_5.2.18-124319_Debian_stretch_amd64.deb 2.安装 .2_5.2.18-124319_Debian_ ...
- Swoole 源码分析——进程管理 Swoole_Process
前言 swoole-1.7.2 增加了一个进程管理模块,用来替代 PHP 的 pcntl 扩展. PHP自带的pcntl,存在很多不足,如 pcntl 没有提供进程间通信的功能 pcntl 不支持重定 ...
- slot插槽(学习笔记)
slot插槽(有默认值,也有名称)一般情况下通过名称进行匹配什么是插槽,有什么用?插槽相当于插入的一个东西,可以用来灵活的封装组件,比如说封装一个模态框对组件进行内容的定制,slot插槽,一对组件标签 ...
- MyBatis学习总结(1)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- ubuntu设置PATH
试了好多遍,多无效.. 最后在/etc/enviroment下设置才有效. 不过让有一些未解问题 我使用sudo su 进入到root用户权限,设置完成的. 重新使用sudo -s进入root用户权限 ...
- java语言MySQL批处理
本质来讲就是使用Statement和PreStatement的addBatch()方法 代码 import java.sql.*; public class GetConnection{ public ...
- NYOJ 915 +-字符串【贪心】
+-字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 Shiva得到了两个仅仅有加号和减号的字符串,字串长度同样.Shiva一次能够把一个加号和它相邻的减号交换 ...
- Android 6.0 开发人员对系统权限的使用与练习(Permissions Best Practices)
Permissions Best Practices 在安装的过程中,用户非常easy忽略权限请求. 假设一个用户相应用感觉沮丧或者操心泄漏个人信息,那么这些用户就会不用他或者卸载它. 怎样规避这个问 ...
- Spark SQL概念学习系列之Spark SQL概述
很多人一个误区,Spark SQL重点不是在SQL啊,而是在结构化数据处理! Spark SQL结构化数据处理 概要: 01 Spark SQL概述 02 Spark SQL基本原理 03 Spark ...
- 【原创】读写锁ReentrantReadWriteLock原理分析(一)
Java里面真正意义的锁并不多,其实真正的实现Lock接口的类就三个,ReentrantLock和ReentrantReadWriteLock的两个内部类(ReentrantReadWriteLock ...