当前位置: 首页 > news >正文

linux环境下常用的查找命令find、which、grep

文章目录

  • 前言
  • find
    • 命令格式
    • 具体示例
  • locate
    • 命令格式
    • 具体示例
  • whereis
    • 命令格式
    • 具体示例
  • which
    • 命令格式
    • 具体示例
  • grep
    • 命令格式
    • 具体示例
  • 总结

前言

查找是运维工作的很重要的一部分,不管是文件查找,还是内容查找,在日常开发维护过程中都常常用到,本文把一些日常用到的查找命令总结到一起,通过对比来学习异同点,进而达到 增强记忆的目的。

本文只是想对常用命令进行一个罗列,并不会对每个命令进行详细的解释,如果想看更详细的用法,直接查询 man 手册是一个不错的选择,我们接下来会说到通用文件查找的 find 命令,快速定位文件的 locate 命令,仅用于搜索程序和文档的 whereis 命令,用于查找系统命令的 which 命令,最后是用于文件内容查找的 grep 命令。

find

命令格式

find [指定目录] 搜索条件 [指定动作]

具体示例

  • 全局查找tendis文件所在目录
[root@VM-0-3-centos ~]# find / -name tendis
/root/tendis
  • 当前目录按指定名找到tendis并打印文件信息
[root@VM-0-3-centos ~]# find . -name tendis -ls
918146    4 drwxr-xr-x   4 root     root         4096 May  1  2021 ./tendis
  • 全局查找test开头的文件
[root@VM-0-3-centos ~]# find / -name 'test*'
/boot/grub2/i386-pc/testspeed.mod
/boot/grub2/i386-pc/test.mod
/boot/grub2/i386-pc/test_blockarg.mod
/boot/grub2/i386-pc/testload.mod
/usr/lib/modules/3.10.0-1127.19.1.el7.x86_64/kernel/drivers/ntb/test
/usr/lib/python2.7/site-packages/jinja2/tests.pyc
/usr/lib/python2.7/site-packages/jinja2/tests.py
/usr/lib/python2.7/site-packages/jinja2/testsuite
...
  • 当前目录下查找所有的目录
[root@VM-0-3-centos ~]# find . -type d
.
./tendis
./tendis/scripts
./tendis/bin
./tendis/bin/deps
./extundelete-0.2.4
...
  • 查找大于1M的文件
[root@VM-0-3-centos ~]# find . -size +1M -ls
918152 164712 -rwxr-xr-x   1 root     root     168663910 Dec 17  2020 ./tendis/bin/tendisplus_static
918151 18036 -rwxr-xr-x   1 root     root     18464898 Dec 17  2020 ./tendis/bin/binlog_tool
918148 2576 -rwxr-xr-x   1 root     root      2635759 Dec 17  2020 ./tendis/bin/redis-cli
918150 10896 -rwxr-xr-x   1 root     root     11154937 Dec 17  2020 ./tendis/bin/deps/libstdc++.so.6
918145 165076 -rwxr-xr-x   1 root     root     169036319 Dec 17  2020 ./tendis/bin/tendisplus
1311915 1860 -rw-r--r--   1 root     root      1904320 Nov 28  2021 ./extundelete-0.2.4/src/extundelete-extundelete.o
1311926 1296 -rwxr-xr-x   1 root     root      1323360 Nov 28  2021 ./extundelete-0.2.4/src/extundelete
...
  • 查找10分钟内修改的普通文件
[root@VM-0-3-centos ~]# find . -type f -mmin -10
./b.txt
./.bash_history

locate

locate 也是用来查找文件的,只不过它不是通过文件系统来找,而是通过自己的数据库来找,默认在 /var/lib/mlocate/mlocate.db,每天自动更新一次,所以查不到最新变动的文件,可以手动通过 updatedb 来更新数据库(我查了一下才2M很小的)。

命令格式

locate [选项] [匹配串]

具体示例

  • 查找家目录下包含te的文件
[root@VM-0-3-centos ~]# locate ~/te
/root/tendis
/root/test.iso
/root/tendis/bin
/root/tendis/file.xml
/root/tendis/scripts
/root/tendis/bin/binlog_tool
/root/tendis/bin/deps
/root/tendis/bin/redis-cli
/root/tendis/bin/tendisplus
/root/tendis/bin/tendisplus_static
/root/tendis/bin/deps/libstdc++.so.6
/root/tendis/scripts/start.sh
/root/tendis/scripts/stop.sh
/root/tendis/scripts/tendisplus.conf
  • 不区分大小写查找
[root@VM-0-3-centos ~]# locate -i ~/tE
/root/TE.txt
/root/tendis
/root/test.iso
/root/tendis/bin
/root/tendis/file.xml
/root/tendis/scripts
/root/tendis/bin/binlog_tool
/root/tendis/bin/deps
/root/tendis/bin/redis-cli
/root/tendis/bin/tendisplus
/root/tendis/bin/tendisplus_static
/root/tendis/bin/deps/libstdc++.so.6
/root/tendis/scripts/start.sh
/root/tendis/scripts/stop.sh
/root/tendis/scripts/tendisplus.conf

whereis

whereis 只能用于二进制文件、man手册和源代码文件的搜索,默认返回所有信息。

命令格式

whereis [-bmsBMS] 匹配串

具体示例

  • 查找二进制程序 ls
[root@VM-0-3-centos ~]# whereis -b ls
ls: /usr/bin/ls
  • 查找 grep 所有信息
[root@VM-0-3-centos ~]# whereis grep
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz

which

which 是在 PATH 变量中找到第一个匹配的命令并返回,这能帮助我们确认多个相同命令时用的是哪一个。

命令格式

which [选项] 匹配串

具体示例

  • 打印当前使用的gcc程序,打印所有可加 -a 参数
[root@VM-0-3-centos ~]# which gcc
/usr/bin/gcc
[root@VM-0-3-centos ~]# which gcc -a
/usr/bin/gcc

grep

grep 不算是单纯查找文件的命令,更多的是用于从文件中过滤指定内容。

命令格式

grep [选项] 匹配串 [指定文件]

具体示例

  • 过滤包含指定字符串的行
[root@VM-0-3-centos ~]# grep "which" w.txt
       which - shows the full path of (shell) commands.
       which [options] [--] programname [...]
       This man page is generated from the file which.texinfo.
  • 显示匹配行之后的2行
[root@VM-0-3-centos ~]# grep "which" w.txt -A 2
       which - shows the full path of (shell) commands.

SYNOPSIS
       which [options] [--] programname [...]

DESCRIPTION
--
       This man page is generated from the file which.texinfo.

OPTIONS
--
  • 当前目录下查找包含 wonderful 的文件
[root@VM-0-3-centos ~]# grep -r "wonderful" .
./.rediscli_history:hset life family wonderful
./.bash_history:grep -r "wonderful" . | head

总结

  • find命令查找文件最全面 find . -name tendis -ls
  • locate 命令查找最快,locate -i /etc/redis,可用 updatedb 命令更新数据库
  • whereis 命令可以查找二进制、man手册、源码,whereis -b grep
  • which 可以从PATH路径下找到第一个匹配的二进制程序
  • grep 一个强大的过滤命令,也可用于找文件 grep -r "wonderful" .

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

幸福感从比较中诞生,亦从比较中消亡,并且与比较双方的关系紧密程度高度相关。我有一块糖,而你没有,我就很幸福,转身发现他有10块糖,然后嘴里的糖瞬间就不甜了~

相关文章:

  • 了解git裸仓库并利用post-receive自动化部署
  • C++17新语法之if和switch语句中可以初始化变量啦
  • C++11中的noexcept说明符和操作符
  • 偶然在github开源项目中发现了.travis.yml这货
  • C++17使用std::optional表示一个可能存在的值
  • 推荐一个C++枚举转字符串的开源项目magic_enum
  • 只问耕耘,不问收获,其实收获却在耕耘中
  • 一个月黑风高的夜晚紧急完成gitlab服务器数据迁移
  • SVN如何删除文件名包含空格的文件
  • std::uniform_real_distribution的一个bug引发的服务器崩溃
  • 参考开源项目实现一个简易的C++枚举转字符串的函数
  • git查看历史记录及修改内容
  • rm -rf 真是删库跑路的一把好手
  • 智能指针(三):weak_ptr浅析
  • float的精度和取值范围
  • Apache Spark Streaming 使用实例
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • gulp 教程
  • JS专题之继承
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • PAT A1092
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 读懂package.json -- 依赖管理
  • 关于 Cirru Editor 存储格式
  • 机器学习中为什么要做归一化normalization
  • 聚簇索引和非聚簇索引
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 力扣(LeetCode)22
  • 算法系列——算法入门之递归分而治之思想的实现
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​Python 3 新特性:类型注解
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (数据结构)顺序表的定义
  • (推荐)叮当——中文语音对话机器人
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • .Family_物联网
  • .htaccess 强制https 单独排除某个目录
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .NET Core跨平台微服务学习资源
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .net中生成excel后调整宽度
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • @reference注解_Dubbo配置参考手册之dubbo:reference
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [20140403]查询是否产生日志
  • [20150321]索引空块的问题.txt
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians