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

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

[[213680]]

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

难度:容易

约定:

  • # - 需要使用 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 的文件。也就是说包含 stretchesstretched 等内容的行也会被显示。 使用 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 命令还会列出所有包含 StretchSTRETCHStReTcH 等内容的文件,也就是说进行的是大小写不敏感的搜索。

  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/*

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2016-12-08 12:47:05

Linux在线主机IP地址

2017-01-06 22:08:32

LinuxShell命令

2019-12-16 09:10:38

Linux中央处理器进程

2019-11-06 15:58:54

Linux内存消耗进程

2019-12-16 11:00:04

LinuxCPU进程

2021-11-24 09:43:11

grepLinux文件

2022-11-01 15:38:22

LinuxShell

2019-12-16 10:43:38

Linux内存消耗进程

2015-03-30 11:34:19

LinuxFSlint

2019-09-26 06:50:16

Linux命令回车字符

2021-08-20 10:46:25

Shell脚本文件Linux

2021-04-21 08:03:34

脚本Shell读取

2022-11-02 08:20:43

Linux

2016-12-16 09:23:29

LinuxShell脚本

2020-03-06 08:56:41

Linux运算符文本

2023-04-17 16:17:19

LinuxPDF

2011-01-26 13:26:32

Linux进程

2022-09-20 14:46:17

PostgreSQL存储工具

2022-03-28 19:53:24

Linux恢复文件意外删除文件

2018-08-27 14:50:46

LinuxShellBash
点赞
收藏

51CTO技术栈公众号