[셸] grep을 사용하여 지정된 텍스트를 포함하는 모든 파일을 찾습니다.

grep은 여러 키워드와 일치합니다.

1. grep'string '

고양이 manpath.config | grep 'MANPATH'

2. 역 매칭

고양이 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 디렉토리를 제외하십시오.

find -name lib -prune -o -name init.c

원본 텍스트 링크 : https://blog.csdn.net/wenwang88/article/details/79254337

(4) grep 이스케이프 문자 "\"검색

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 네트워크

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

 

 

 

목표 : 이 문서에서는 지정된 디렉터리 또는 전체 파일 시스템에서 지정된 단어 나 문자열이 포함 된 파일을 검색하는 방법에 대한 정보를 제공합니다.

난이도 : 쉬움

협약:

  • #-지정된 명령 을 실행하려면 루트 권한을 사용해야합니다. 루트 사용자를 사용하여 직접 실행하거나 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

대소 문자를 구분하지 않는 검색

기본적으로 검색은 대소 문자를 구분합니다. 즉, 문자열 stretch를 검색 할 때 대소 문자 내용이 동일한 파일 만 포함됩니다.

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