Linux文件搜索命令学习
你必须非常努力,才能看起来毫不费力!
微信搜索公众号[ 漫漫Coding路 ],一起From Zero To Hero !
前言
本篇文章主要讨论Linux中的文件搜索命令,大家一起来学习吧!
find
命令格式
find 路径 选项 参数 [ -exec -ok command {} \;]
命令描述
- 用来在指定目录下查找文件
- 不填写路径,默认在当前目录下查找
- 如果不使用选项参数,则find命令将在当前目录下查找子目录与文件,并且将查找到的子目录和文件全部进行显示
常用选项
- -name:指定文件名
- -iname:不区分大小写
- -size:文件大小,如
+ - 10M
(大于小于文件大小) - -user:根据所有者查找
- -group:根据所属组查找
- -amin:(modify)文件的访问时间 ,使用
+ - 时间
(大于小于多长时间内的) - -cmin:(change)文件属性被更改时间 ,使用
+ - 时间
(大于小于多长时间内的) - -mmin:(modify)文件内容被修改时间 ,使用
+ - 时间
(大于小于多长时间内的) - -type:文件类型(f 文件 d 目录 l 链接)
- -inum:根据i节点查找
- -a 连接多个条件 (and)
- -o:连接多个条件(or)
-exec command {} \;
: 对执行结果执行提供的command操作直接执行,例如ls,后面的{} \;
是固定写法-ok command {} \;
: 对执行结果执行提供的command操作询问执行,例如ls,后面的{} \;
是固定写法
示例
# 1. 文件准备
[root@VM-0-5-centos tmp]# cd /opt
[root@VM-0-5-centos tmp]# mkdir test
[root@VM-0-5-centos tmp]# cd test/
# 创建两个文件以及两个文件夹
[root@VM-0-5-centos test]# touch test
[root@VM-0-5-centos test]# touch TEST
[root@VM-0-5-centos test]# mkdir test1
[root@VM-0-5-centos test]# mkdir 1test
[root@VM-0-5-centos test]# ls -l
总用量 8
drwxr-xr-x 2 root root 4096 10月 9 20:45 1test
-rw-r--r-- 1 root root 0 10月 9 20:45 test
-rw-r--r-- 1 root root 0 10月 9 20:45 TEST
drwxr-xr-x 2 root root 4096 10月 9 20:45 test1# 2. 使用-name查找
[root@VM-0-5-centos test]# find -name test
./test# 支持使用通配符: ?匹配单个字符,*匹配多个字符
[root@VM-0-5-centos test]# find -name tes?
./test[root@VM-0-5-centos test]# find -name '*tes*'
./test1
./1test
./test# 3. 使用 -iname,不区分大小写
[root@VM-0-5-centos test]# find -iname test
./TEST
./test# 4. 使用 -size,查找大于1M的文件
[root@VM-0-5-centos opt]# find -size +1M
./redis-6.2.4.tar.gz
./redis-6.2.4/src/redis-benchmark# 5. 根据所有者查找
[root@VM-0-5-centos opt]# find /home -user lifelmy
/home/lifelmy
/home/lifelmy/.bashrc# 6. 使用mmin,查找30min以内,内容被改变过的(cmin、amin类似)
[root@VM-0-5-centos opt]# find /tmp -mmin -30
/tmp
/tmp/stargate.lock
/tmp/test
/tmp/test/test1
/tmp/test/TEST
/tmp/test/1test
/tmp/test/test# 7. 使用条件语句连接(30min以内,内容被改变过的文件,不包含目录)
[root@VM-0-5-centos opt]# find /tmp -mmin -30 -a -type d
/tmp
/tmp/test
/tmp/test/test1
/tmp/test/1test# 8. 使用 -exec command {} \;
[root@VM-0-5-centos opt]# find /tmp -mmin -30 -a -type f -a -exec ls -l {} \;
-rw-r--r-- 1 root root 0 10月 9 20:46 /tmp/stargate.lock
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/TEST
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/test# 8. 使用 -ok command {} \;
[root@VM-0-5-centos tmp]# find /tmp -mmin -50 -a -type f -ok ls -l {} \;
< ls ... /tmp/stargate.lock > ? n
< ls ... /tmp/test/TEST > ? y
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/TEST
< ls ... /tmp/test/test > ? y
-rw-r--r-- 1 root root 0 10月 9 20:45 /tmp/test/test# 9. 通过-inum查找到文件,然后删除(适用于那种文件名过长,或者不符合规范的文件名(有空格))
[root@VM-0-5-centos tmp]# ls -li
总用量 4
991 -rw-r--r-- 1 root root 0 10月 9 20:46 stargate.lock
264 drwxr-xr-x 4 root root 4096 10月 9 20:45 test
[root@VM-0-5-centos tmp]# find /tmp -inum 991 -ok rm {} \;
< rm ... /tmp/stargate.lock > ? y
[root@VM-0-5-centos tmp]# ls
test
locate
命令格式
locate [选项] [样式]
命令描述
- 与
find
挨个查找不同,locate
在本地维护了一个数据库,通过该命令查找不用挨个比对,比较节省资源。 - 但是
locate
是定期维护的,新建立的文件有可能不能立即查找到,可以使用updatedb
命令更新数据库,就可以查找到了。 locate
不会维护/tmp
目录下的文件
常用选项
- -c, --count # 只输出找到的数量
- -r, --regexp REGEXP # 使用基本正则表达式
- -i, --ignore-case # 忽略大小写
示例
[root@VM-0-5-centos tmp]# locate /opt/redis-6.2.4/redis.
/opt/redis-6.2.4/redis.conf
which
命令格式
which 命令
命令描述
搜索命令所在目录及别名信息
示例
[root@VM-0-5-centos tmp]# which ls
alias ls='ls --color=auto'/usr/bin/ls
whereis
命令格式
whereis 命令
命令描述
搜索命令所在目录及帮助文档路径
示例
[root@VM-0-5-centos tmp]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
grep
命令格式
grep -iv [指定字符串] [文件]
命令描述
- 在文件中搜寻字符串匹配的行并输出
- 可以使用正则表达式或通配符来指定字符串
选项
- -i 不区分大小写
- -v 排除指定字符串
示例
[root@a698b274ec6d test]# grep love test.txt
i love you[root@a698b274ec6d test]# grep -i love test.txt
i love you
i LOVE you[root@a698b274ec6d test]# grep -v *love* test.txt
i hate you
i LOVE you[root@a698b274ec6d test]# grep -i -v *love* test.txt
i hate you[root@VM-0-5-centos tmp]# grep ^i test.txt
i love you
i LOVE you
i hate you
总结
本文介绍了Linux部分文件搜索命令:
- find:在指定目录下查找文件
- locate:使用本地维护的数据库进行查询,数据库会定时更新
- which:搜索命令所在目录及别名信息
- whereis:搜索命令所在目录及帮助文档路径
- grep:找文件里符合条件的字符串
更多
个人博客: https://lifelmy.github.io/
微信公众号:漫漫Coding路
Linux文件搜索命令学习相关推荐
- linux搜索命令有哪些,linux五大搜索命令学习
五大搜索命令学习 分别解释locate,find,which,whereis,grep 五大linux搜索命令 locate 解释:由man手册可以看出,locate查找就是根据文件名进行查找,只是依 ...
- linux的文件搜索命令,Linux文件搜索命令find的用法 | 术与道的分享
不管在Windows还是Linux中,最重要的问题不是说你能搜索到这个文件,而是最好少用搜索,应该是你在整个服务器的规划里面,把所以的文件目录规划的很好.就像如果你在家里找衣服,如果不是你乱扔,就不可 ...
- Linux文件搜索命令介绍——locate、find、xargs、touch、stat
本文主要介绍两个用在Linux系统中搜索文件的工具. locate:通过文件名查找文件 find:在文件系统目录框架中查找文件 同时,我们也会介绍一个通常与文件搜索命令一起使用.处理搜索结果文件列表的 ...
- Linux文件搜索命令find:根据文件名搜索、根据文件大小搜索、根据文件所有者(所属组)搜索、根据访问时间搜索、查找并显示文件、根据文件类型搜索、根据i节点搜索
文件搜索命令 一.基本定义 二.根据文件名搜索(*匹配任意字符,?匹配单个字符) 三.根据文件大小搜索(+n大于.-n小于.n等于) 四.根据文件所有者(所属组)搜索 五.根据访问时间搜索 六.满足多 ...
- Linux文件搜索命令
1.find:在指定目录下查找文件 find命令对文件进行搜索的时候完全匹配文件名,find命令对文件进行搜索的时候完全匹配文件名 通配符有:*:匹配任意内容:?: 匹配一个任意字符:[]:匹配中括号 ...
- Linux文件搜索命令find、which和whereis应用
记录:349 场景:在CentOS 7.9操作系统上,使用find命令在指定目录搜索文件,支持精确搜索和模糊搜索:使用which命令搜索一个命令所在的目录和别名信息.使用whereis命令搜索命令所在 ...
- find name 模糊匹配_Linux的文件搜索命令(locate ,find,grep,find命令和)
刚开始学Linux,这是关于Linux文件搜索命令,就目前,尽我所能把他写全一点,后期随时补充 文件搜索命令 一.locate命令 二.find命令 三.grep命令 四.find命令和grep命令的 ...
- Linux常用指令学习(篇三):文件搜索命令
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.文件搜索命令:find 1. 根据文件名搜索 -name 2. 指定文件大小搜索 -size 3. 根据所有者搜索 ...
- Linux学习4之常用命令--文件搜索命令
一.文件搜索命令 find 语法:find [搜索范围] [匹配条件] {find [搜索范围] [搜索模式] [搜索条件]} 1.以文件名来查找 -name(区分大小写)-iname (不区分 ...
最新文章
- python 直方图的绘制方法全解_python plotly绘制直方图实例详解
- RocketMQ Docker安装
- linux上很方便的上传下载文件工具rz和sz使用介绍
- linux之gdb基本调试命令和使用总结
- 2.项目介绍,数据库迁移(code first)
- python可以用于工业机器人编程与操作_工业机器人用什么语言编程?
- 不同文件格式打开的方法
- xml文件怎么转换成wps_Office 12使用XML格式存储文件 正式回击WPS
- 计算机毕业设计 网吧计费管理系统 网吧管理系统 网吧管理系统java 网吧上机管理系统 网吧计费系统 网吧系统 网吧收费管理系统 网咖系统 网咖管理系统 网咖计费系统 网咖收费管理系统
- 2023福州大学计算机考研信息汇总
- 用友U8V10.1软件安装教程
- linux中yum -y install mysql为什么默认是mariadb?以及mysql yum源的配置
- 开挂?16岁保送北大、MIT量子计算机博士是这样玩转区块链的!
- ubuntu10.10下smbd服务配置——实现主机WinXP与虚拟机Ubuntu10.10共享
- 苹果退款_苹果退款流程最新苹果内购退款流程和教程
- 漫画人物头像总是画不好?快看看这些注意点你有没有中招!
- python爬虫中字符串开头b,u,r的含义
- Linux xorg 调整分辨率,编写xorg.conf解决Ubuntu分辨率不可调的问题
- [zz] Android五大布局Layout详解
- Bluecms代码审计