自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

系統(tǒng) Linux
本文提供一些關(guān)于如何搜索出指定目錄或整個文件系統(tǒng)中那些包含指定單詞或字符串的文件。

[[213680]]

目標:本文提供一些關(guān)于如何搜索出指定目錄或整個文件系統(tǒng)中那些包含指定單詞或字符串的文件。

難度:容易

約定:

  • # - 需要使用 root 權(quán)限來執(zhí)行指定命令,可以直接使用 root 用戶來執(zhí)行也可以使用 sudo 命令
  • $ - 可以使用普通用戶來執(zhí)行指定命令

 

案例

 

非遞歸搜索包含指定字符串的文件

***個例子讓我們來搜索 /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 選項會在發(fā)現(xiàn)不存在或者不能讀取的文件時隱藏報錯信息。結(jié)果顯示除了文件名之外,還有包含請求字符串的行也被一起輸出了。

 

遞歸地搜索包含指定字符串的文件

上面案例中忽略了所有的子目錄。所謂遞歸搜索就是指同時搜索所有的子目錄。

下面的命令會在 /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 等內(nèi)容的行也會被顯示。 使用 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)"

 

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

上面的命令都會產(chǎn)生多余的輸出。下一個案例則會遞歸地搜索 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 時只會包含大小寫一致內(nèi)容的文件。

通過使用 grep-i 選項,grep 命令還會列出所有包含 Stretch , STRETCH , StReTcH 等內(nèi)容的文件,也就是說進行的是大小寫不敏感的搜索。

  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中國
相關(guān)推薦

2016-12-08 12:47:05

Linux在線主機IP地址

2017-01-06 22:08:32

LinuxShell命令

2022-11-01 15:38:22

LinuxShell

2019-12-16 10:43:38

Linux內(nèi)存消耗進程

2015-03-30 11:34:19

LinuxFSlint

2021-11-24 09:43:11

grepLinux文件

2019-11-06 15:58:54

Linux內(nèi)存消耗進程

2019-12-16 09:10:38

Linux中央處理器進程

2019-12-16 11:00:04

LinuxCPU進程

2021-04-21 08:03:34

腳本Shell讀取

2021-08-20 10:46:25

Shell腳本文件Linux

2019-09-26 06:50:16

Linux命令回車字符

2016-12-16 09:23:29

LinuxShell腳本

2022-11-02 08:20:43

Linux

2019-03-18 13:00:15

LinuxFish ShellBash

2011-01-26 13:26:32

Linux進程

2020-03-06 08:56:41

Linux運算符文本

2023-04-17 16:17:19

LinuxPDF

2021-08-30 13:08:54

Linux實時監(jiān)控日志文件

2018-12-28 10:45:08

Linux文本行命令
點贊
收藏

51CTO技術(shù)棧公眾號