zoukankan      html  css  js  c++  java
  • Linux小技巧:find . | xargs grep -ri "关键字" -l —— 查询、匹配文件内容关键词

    大家在工作中,是否遇到需要查询某个文件下内容中的关键词是否存在。例如:

    需要查询在/etc/passwd下是否存在“yasuo”关键字:

    [root@localhost ~]# find /etc/passwd | xargs grep -ri 'yasuo'
    yasuo:x:1001:1001::/home/yasuo:/bin/bash
    yasuo_1:x:1234:1010::/home/yasuo_1:/sbin/nologin
    yasuo_2:x:1011:1011::/media/yasuo_2:/bin/bash
    如果只需要知道文件名称:

    [root@localhost ~]# find /etc/passwd | xargs grep -ri 'yasuo' -l
    /etc/passwd
     

    我们在1.txt文件中放入‘123’,2.txt中放入‘456’

    [root@localhost 1]# echo 456 >2.txt
    [root@localhost 1]# echo 123 >1.txt
    [root@localhost 1]# tree
    .
    ├── 1.txt
    └── 2.txt

    0 directories, 2 files
    使用命令查询文件中存在关键字2的文件:

    [root@localhost 1]# find .| xargs grep -ri '2'
    ./1.txt:123
    ./1.txt:123
    加上 -l 只显示文件名称

    [root@localhost 1]# find .| xargs grep -ri '2' -l
    ./1.txt
    ./1.txt
    如果没有则不显示任何信息

    [root@localhost 1]# find .| xargs grep -ri '9' -l
    [root@localhost 1]# find .| xargs grep -ri '9'
    [root@localhost 1]#
     
    ————————————————
    版权声明:本文为CSDN博主「傲娇天子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_41116956/article/details/82788018

  • 相关阅读:
    数据安全-数据加密学
    网络文件共享服务-SAMBA服务搭建和配置
    vsftp
    网络文件共享服务-NFS服务配置和测试
    负载均衡,主备模式
    系统中查找文件
    如何管理Linux系统的进程与计划任务
    Robot Framework 发送接口示例
    unittest官网文档
    macOS搭建Selenium Server(Selenium Grid)
  • 原文地址:https://www.cnblogs.com/sandshell/p/12808122.html
Copyright © 2011-2022 走看看