[シェル] grepを使用して、指定したテキストを含むすべてのファイルを検索します

grepは複数のキーワードに一致します

1. grep'string '

cat manpath.config | grep'MANPATH '

2.リバースマッチング

cat manpath.config | grep -v'MANPATH '

3.複数のキーワードを同時に一致させる

grep -E "word1 | word2 | word3" file.txt

4.複数のキーワードの同時逆マッチング

grep -vE "word1 | word2 | word3" file.txt

 

 

grep検索ファイルの内容の詳細な説明

https://www.jb51.net/article/127783.htm

(1)ファイルタイプを指定します
find -type f -name '* .h' | xargs grep "hello"


(2)javaおよびcファイルタイプを除外します
grep "hello" -nR --exclude = *。{java、c}

(3)ファイルinit.c見つけるためにlibディレクトリを除外します



元のテキストへの-namelib -prune -o -name init.cリンクを検索します:https://blog.csdn.net/wenwang88/article/details/79254337

(4)grepエスケープ文字 "\" search

https://blog.csdn.net/chengf223/article/details/85229097

(5)非再帰的検索

grep -s "stretch "/etc/*

-s 选项会在发现不存在或者不能读取的文件时隐藏报错信息。

(5)再帰検索

grep -R "stretch" / etc / *

 

https://www.linuxprobe.com/grep-find-file.html

 

grepが機能しませんか?フィルタリングまたは表示

service --status-all | grep network、grep「network」文字列のみにパイプした場合でも、すべてのサービスが一覧表示されます

解決:

service --status-all |&grep network

https://ubuntuqa.com/article/10818.html

 

 

 

目的:この記事では、指定されたディレクトリまたはファイルシステム全体で、指定された単語または文字列を含むファイルを検索する方法について説明します。

難易度:簡単

コンベンション:

  • #-指定したコマンドを実行するには、root権限を使用する必要があります。rootユーザーを使用して直接実行するか、sudoコマンドを使用できます。 
  • $-通常のユーザーを使用して、指定したコマンドを実行できます

ケーススタディ

指定された文字列を含むファイルの非再帰的検索

最初の例では、/ etc /ディレクトリ内の文字列stretchを含むすべてのファイルを検索しますが、サブディレクトリは検索しません。

# grep -s stretch /etc/*
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"

grepの-sオプションは、存在しないファイルまたは読み取ることができないファイルが見つかった場合にエラーメッセージを非表示にします。結果は、ファイル名に加えて、要求された文字列を含む行も出力されることを示しています。

指定された文字列を含むファイルを再帰的に検索します

上記の場合、すべてのサブディレクトリは無視されます。いわゆる再帰検索とは、すべてのサブディレクトリを同時に検索することを指します。

次のコマンドは、/ etc /とそのサブディレクトリに文字列stretchを含むファイルを検索します。

# grep -R stretch /etc/*
/etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main
/etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main
/etc/dictionaries-common/words:backstretch
/etc/dictionaries-common/words:backstretch's
/etc/dictionaries-common/words:backstretches
/etc/dictionaries-common/words:homestretch
/etc/dictionaries-common/words:homestretch's
/etc/dictionaries-common/words:homestretches
/etc/dictionaries-common/words:outstretch
/etc/dictionaries-common/words:outstretched
/etc/dictionaries-common/words:outstretches
/etc/dictionaries-common/words:outstretching
/etc/dictionaries-common/words:stretch
/etc/dictionaries-common/words:stretch's
/etc/dictionaries-common/words:stretched
/etc/dictionaries-common/words:stretcher
/etc/dictionaries-common/words:stretcher's
/etc/dictionaries-common/words:stretchers
/etc/dictionaries-common/words:stretches
/etc/dictionaries-common/words:stretchier
/etc/dictionaries-common/words:stretchiest
/etc/dictionaries-common/words:stretching
/etc/dictionaries-common/words:stretchy
/etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"

特定の単語を含むすべてのファイルを検索する

上記のgrepコマンドの場合、文字列stretchを含むすべてのファイルが一覧表示されます。つまり、ストレッチ、ストレッチなどを含む行も表示されます。grepの-wオプションを使用すると、特定の単語を含む行のみが表示されます。

# grep -Rw stretch /etc/*
/etc/apt/sources.list:# deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:#deb cdrom:[Debian GNU/Linux testing _Stretch_ - Official Snapshot amd64 NETINST Binary-1 20170109-05:56]/ stretch main
/etc/apt/sources.list:deb http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb-src http://ftp.au.debian.org/debian/ stretch main
/etc/apt/sources.list:deb http://security.debian.org/debian-security stretch/updates main
/etc/apt/sources.list:deb-src http://security.debian.org/debian-security stretch/updates main
/etc/dictionaries-common/words:stretch
/etc/dictionaries-common/words:stretch's
/etc/grub.d/00_header:background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
/etc/os-release:PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
/etc/os-release:VERSION="9 (stretch)"

特定のテキストを含むファイル名を表示する

上記のコマンドはすべて冗長な出力を生成します。次のケースでは、etcディレクトリでstretchを含むファイルを再帰的に検索し、ファイル名のみを出力します。

# grep -Rl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release

大文字と小文字を区別しない検索

デフォルトでは、検索では大文字と小文字が区別されます。つまり、文字列ストレッチを検索すると、大文字と小文字が同じ内容のファイルのみが含まれます。

grepの-iオプションを使用すると、grepコマンドは、Stretch、STRETCH、StReTcHなどを含むすべてのファイルも一覧表示します。これは、検索で大文字と小文字が区別されないことを意味します。

# grep -Ril stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/default.hash
/etc/dictionaries-common/words
/etc/grub.d/00_header
/etc/os-release

検索時に指定したファイルを含める/除外する

grepコマンドは、指定されたファイルのみを検索することもできます。たとえば、構成ファイル(拡張子.conf)で指定されたテキスト/文字列のみを検索できます。次の例では、/ etcディレクトリで文字列bashと拡張子.confを持つすべてのファイルを検索します。

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

同様に、-excludeを使用して特定のファイルを除外することもできます。

# grep -Ril --exclude=\*.conf bash /etc/*
/etc/alternatives/view
/etc/alternatives/vim
/etc/alternatives/vi
/etc/alternatives/vimdiff
/etc/alternatives/rvim
/etc/alternatives/ex
/etc/alternatives/rview
/etc/bash.bashrc
/etc/bash_completion.d/grub
/etc/cron.daily/apt-compat
/etc/cron.daily/exim4-base
/etc/dictionaries-common/default.hash
/etc/dictionaries-common/words
/etc/inputrc
/etc/passwd
/etc/passwd-
/etc/profile
/etc/shells
/etc/skel/.profile
/etc/skel/.bashrc
/etc/skel/.bash_logout

検索時に指定されたディレクトリを除外する

ファイルと同様に、grepは検索時に指定されたディレクトリを除外することもできます。--exclude-dirオプションを使用するだけです。

次の例では、/ etcディレクトリ内の文字列stretchを含むファイルを検索しますが、/ etc /grub.dディレクトリ内のファイルは検索しません。

# grep --exclude-dir=/etc/grub.d -Rwl stretch /etc/*
/etc/apt/sources.list
/etc/dictionaries-common/words
/etc/os-release

検索文字列を含む行番号を表示します

-nオプションは、指定された文字列が配置されている行の行番号も表示します。

# grep -Rni bash /etc/*.conf
/etc/adduser.conf:6:DSHELL=/bin/bash

指定された文字列を含まないファイルを検索する

この最後の例では、-vを使用して、指定された文字列を含まないすべてのファイルを一覧表示します。

たとえば、次のコマンドは、/ etcディレクトリにstretchを含まないすべてのファイルを検索します。

# grep -Rlv stretch /etc/*

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

おすすめ

転載: blog.csdn.net/bandaoyu/article/details/114755024