본문 바로가기

StackOverflow

[Linux] 전체 시스템에서, 특정 스트링을 포함하는 파일 찾기

http://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-on-linux


Q: 제 리눅스 시스템 전체에서 특정 스트링을 포함한 파일을 찾고 싶습니다. 이 스트링은 파일 내에 있는 것이고, 파일명은 아닙니다. 어떻게 하면 될까요?

(질문자: Nathan)





A: 이렇게 하세요.

grep -rnw '/path/to/somewhere/' -e "pattern"

-r 또는 -R 은 리커시브하게 하위 폴더까지 검색하는 옵션이고, -n 은 라인넘버, 그리고 -w 는 모든 단어가 일치해야 한다는 옵션입니다. . -l (영문 L) 옵션을 추가하면 파일 명까지 지정해 줄 수 있습니다..

이것과 함께, --exclude 또는 --include 파라메터를 써서 더 효율적으로 검색을 할 수 있습니다. 아래와 같이요.

grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

이 코드를 이용하면 확장자가 .c 또는 .h 인 파일만 찾아줍니다. --exclude도 비슷한데,

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

이 코드를 이용하면 .o 확장자를 빼고 검색해 줍니다. 파일을 포함/제외하는 것과 마찬가지로, 디렉토리를 --exclude-dir 그리고 --include-dir 파라메터를 이용하여 포함/제외 시킬 수 있습니다. --exclude-dir는 아래와 같이 이용할 수 있습니다.

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

제 리눅스 환경에서는 이 코드가 아주 잘 동작합니다. 당신과 같은 경우에 이용하기 적절한 코드입니다.

옵션을 더 확인하려면 아래와 같이 입력하세요.

man grep

(답변자: rakib)




A:  grep -ilR을 이용하세요.

grep -Ril "text-to-find-here" /
  • i 대소문자 무시
  • R 리커시브하게 하위 폴더 까지 검색
  • l 결과 값은 제외하고, 결과 파일명만 출력.


(답변자: fedrogui )


I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text. Just to clarify, I'm looking for text within the file, not in the file name.

When I was looking up how to do this, I came across this solution twice:

find / -type f -exec grep -H 'text-to-find-here' {} \;

However, it doesn't work. It seems to display every single file in the system.

Is this close to the proper way to do it? If not, how should I? This ability to find text strings in files would be extraordinary useful for me for some programming projects I'm doing.

shareeditflag


_



1649down voteaccepted

Do the following:

grep -rnw '/path/to/somewhere/' -e "pattern"

-r or -R is recursive, -n is line number and -w stands match the whole word. -l (letter L) can be added to have just the file name.

Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:

grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

Above will exclude searching all the files ending with .o extension. Just like exclude file it's possible to exclude/include directories through --exclude-dir and --include-dir parameter, the following shows how to integrate --exclude-dir:

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

This works for me very well, to achieve almost the same purpose like yours.

For more options :

man grep
shareeditflag



_

You can use grep -ilR:

grep -Ril "text-to-find-here" /
  • i stands for ignore case (optional in your case).
  • R stands for recursive.
  • l stands for "show the file name, not the result itself`.
shareeditflag