如何在 Linux shell 中找出所有包含指定文本的文件

来源:互联网 发布:mac 菜单栏图标隐藏 编辑:程序博客网 时间:2024/05/16 07:05
本文提供一些关于如何搜索出指定目录或整个文件系统中那些包含指定单词或字符串的文件。
-- Lubos Rendek

本文导航
编译自 | https://linuxconfig.org/how-to-find-all-files-with-a-specific-text-using-linux-shell 
 作者 | Lubos Rendek
 译者 | lujun9972

目标:本文提供一些关于如何搜索出指定目录或整个文件系统中那些包含指定单词或字符串的文件。

难度:容易

约定:

◈ # - 需要使用 root 权限来执行指定命令,可以直接使用 root 用户来执行也可以使用 sudo 命令
◈ $ - 可以使用普通用户来执行指定命令

案例

非递归搜索包含指定字符串的文件

第一个例子让我们来搜索 /etc/ 目录下所有包含 stretch 字符串的文件,但不去搜索其中的子目录:

  1. # grep -s stretch /etc/*

  2. /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"

  3. /etc/os-release:VERSION="9 (stretch)"

grep 的 -s 选项会在发现不存在或者不能读取的文件时隐藏报错信息。结果显示除了文件名之外,还有包含请求字符串的行也被一起输出了。

递归地搜索包含指定字符串的文件

上面案例中忽略了所有的子目录。所谓递归搜索就是指同时搜索所有的子目录。

下面的命令会在 /etc/ 及其子目录中搜索包含 stretch 字符串的文件:

  1. # grep -R stretch /etc/*

  2. /etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main

  3. /etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main

  4. /etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main

  5. /etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main

  6. /etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main

  7. /etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main

  8. /etc/dictionaries-common/words:backstretch

  9. /etc/dictionaries-common/words:backstretch's

  10. /etc/dictionaries-common/words:backstretches

  11. /etc/dictionaries-common/words:homestretch

  12. /etc/dictionaries-common/words:homestretch's

  13. /etc/dictionaries-common/words:homestretches

  14. /etc/dictionaries-common/words:outstretch

  15. /etc/dictionaries-common/words:outstretched

  16. /etc/dictionaries-common/words:outstretches

  17. /etc/dictionaries-common/words:outstretching

  18. /etc/dictionaries-common/words:stretch

  19. /etc/dictionaries-common/words:stretch's

  20. /etc/dictionaries-common/words:stretched

  21. /etc/dictionaries-common/words:stretcher

  22. /etc/dictionaries-common/words:stretcher's

  23. /etc/dictionaries-common/words:stretchers

  24. /etc/dictionaries-common/words:stretches

  25. /etc/dictionaries-common/words:stretchier

  26. /etc/dictionaries-common/words:stretchiest

  27. /etc/dictionaries-common/words:stretching

  28. /etc/dictionaries-common/words:stretchy

  29. /etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`

  30. /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"

  31. /etc/os-release:VERSION="9 (stretch)"

搜索所有包含特定单词的文件

上面 grep 命令的案例中列出的是所有包含字符串 stretch 的文件。也就是说包含 stretches , stretched 等内容的行也会被显示。 使用 grep 的 -w 选项会只显示包含特定单词的行:

  1. # grep -Rw stretch /etc/*

  2. /etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main

  3. /etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main

  4. /etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main

  5. /etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main

  6. /etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main

  7. /etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main

  8. /etc/dictionaries-common/words:stretch

  9. /etc/dictionaries-common/words:stretch's

  10. /etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`

  11. /etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"

  12. /etc/os-release:VERSION="9 (stretch)"

显示包含特定文本的文件名

上面的命令都会产生多余的输出。下一个案例则会递归地搜索 etc 目录中包含 stretch的文件并只输出文件名:

  1. # grep -Rl stretch /etc/*

  2. /etc/apt/sources.list

  3. /etc/dictionaries-common/words

  4. /etc/grub.d/00_header

  5. /etc/os-release

大小写不敏感的搜索

默认情况下搜索是大小写敏感的,也就是说当搜索字符串 stretch 时只会包含大小写一致内容的文件。

通过使用 grep 的 -i 选项,grep 命令还会列出所有包含 Stretch , STRETCH, StReTcH 等内容的文件,也就是说进行的是大小写不敏感的搜索。

  1. # grep -Ril stretch /etc/*

  2. /etc/apt/sources.list

  3. /etc/dictionaries-common/default.hash

  4. /etc/dictionaries-common/words

  5. /etc/grub.d/00_header

  6. /etc/os-release

搜索时包含/排除指定文件

grep 命令也可以只在指定文件中进行搜索。比如,我们可以只在配置文件(扩展名为.conf)中搜索指定的文本/字符串。 下面这个例子就会在 /etc 目录中搜索带字符串 bash 且所有扩展名为 .conf 的文件:

  1. # grep -Ril bash /etc/*.conf

  2. OR

  3. # grep -Ril --include=\*.conf bash /etc/*

  4. /etc/adduser.conf

类似的,也可以使用 --exclude 来排除特定的文件:

  1. # grep -Ril --exclude=\*.conf bash /etc/*

  2. /etc/alternatives/view

  3. /etc/alternatives/vim

  4. /etc/alternatives/vi

  5. /etc/alternatives/vimdiff

  6. /etc/alternatives/rvim

  7. /etc/alternatives/ex

  8. /etc/alternatives/rview

  9. /etc/bash.bashrc

  10. /etc/bash_completion.d/grub

  11. /etc/cron.daily/apt-compat

  12. /etc/cron.daily/exim4-base

  13. /etc/dictionaries-common/default.hash

  14. /etc/dictionaries-common/words

  15. /etc/inputrc

  16. /etc/passwd

  17. /etc/passwd-

  18. /etc/profile

  19. /etc/shells

  20. /etc/skel/.profile

  21. /etc/skel/.bashrc

  22. /etc/skel/.bash_logout

搜索时排除指定目录

跟文件一样,grep 也能在搜索时排除指定目录。 使用 --exclude-dir 选项就行。

下面这个例子会搜索 /etc 目录中搜有包含字符串 stretch 的文件,但不包括 /etc/grub.d 目录下的文件:

  1. # grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/*

  2. /etc/apt/sources.list

  3. /etc/dictionaries-common/words

  4. /etc/os-release

显示包含搜索字符串的行号

-n 选项还会显示指定字符串所在行的行号:

  1. # grep -Rni bash /etc/*.conf

  2. /etc/adduser.conf:6:DSHELL=/bin/bash

寻找不包含指定字符串的文件

最后这个例子使用 -v 来列出所有包含指定字符串的文件。

例如下面命令会搜索 /etc 目录中不包含 stretch 的所有文件:

  1. # grep -Rlv stretch /etc/*


via: https://linuxconfig.org/how-to-find-all-files-with-a-specific-text-using-linux-shell

作者:Lubos Rendek[2] 译者:lujun9972 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

LCTT 译者
lujun9972 ? ? ?
共计翻译:24 篇
贡献时间:21 天

推荐文章

< 左右滑动查看相关文章 >

点击图片、输入文章 ID 或识别二维码直达


阅读全文
0 0