成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何在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命令

2022-11-01 15:38:22

LinuxShell

2019-12-16 10:43:38

Linux內存消耗進程

2019-11-06 15:58:54

Linux內存消耗進程

2019-12-16 09:10:38

Linux中央處理器進程

2019-12-16 11:00:04

LinuxCPU進程

2021-11-24 09:43:11

grepLinux文件

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讀取

2016-12-16 09:23:29

LinuxShell腳本

2022-11-02 08:20:43

Linux

2011-01-26 13:26:32

Linux進程

2020-03-06 08:56:41

Linux運算符文本

2023-04-17 16:17:19

LinuxPDF

2019-03-18 13:00:15

LinuxFish ShellBash

2022-03-28 19:53:24

Linux恢復文件意外刪除文件

2018-08-27 14:50:46

LinuxShellBash
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91免费版在线观看 | 成人欧美一区二区三区在线播放 | 欧美一级在线免费 | 国产精品免费一区二区 | 欧美黄色小视频 | 国产精品久久 | 日韩在线视频网址 | 成人av播放 | 欧美成年网站 | 日本在线免费视频 | 一二区视频 | 久久久久久久久久影视 | 精品一区二区三区四区外站 | 国产高清久久久 | 一区二区三区国产 | 久久亚 | 国产免费又色又爽又黄在线观看 | 欧产日产国产精品国产 | www久久久| 91在线中文字幕 | 国精产品一区二区三区 | av影片在线 | 国产成人精品一区二区三区 | 美女久久久久久久久 | 久久精品亚洲精品国产欧美 | www.国产 | 亚洲www啪成人一区二区 | 欧美一级二级视频 | 亚洲乱码一区二区三区在线观看 | 精品国产乱码久久久 | 国产福利在线播放 | 亚洲国产精品日韩av不卡在线 | 国产成人高清成人av片在线看 | 亚洲欧美国产视频 | 国产成人精品久久 | 99久久久国产精品免费消防器 | 伊人网站 | 91xxx在线观看 | 自拍偷拍视频网 | av一二三区| 中文字幕久久精品 |