etc./StackOverFlow

Linux에서 grep을 사용하여 파일 이름만 표시하려면 어떻게 해야 합니까?

청렴결백한 만능 재주꾼 2023. 4. 26. 12:46
반응형

질문자 :cwd


Linux에서 grep 을 사용하여 파일 이름(인라인 일치 없음)만 표시하려면 어떻게 해야 합니까?

나는 일반적으로 다음과 같은 것을 사용하고 있습니다.

 find . -iname "*php" -exec grep -H myString {} \;

어떻게 파일 이름(경로 포함)을 얻을 수 있지만 일치 항목은 없을까요? xargs 를 사용해야 합니까? 내 grep 매뉴얼 페이지에서 이 작업을 수행하는 방법을 보지 못했습니다.



표준 옵션 grep -l (소문자 L)이 이를 수행할 수 있습니다.

유닉스 표준에서 :

 -l (The letter ell.) Write only the names of files containing selected lines to standard output. Pathnames are written once per file searched. If the standard input is searched, a pathname of (standard input) will be written, in the POSIX locale. In other locales, standard input may be replaced by something more appropriate in those locales.

이 경우 -H 도 필요하지 않습니다.


Random832

grep(1) 매뉴얼 페이지에서:

 -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)

Ignacio Vazquez-Abrams

간단한 파일 검색을 위해 grep의 -l-r 옵션을 사용할 수 있습니다.

 grep -rl "mystring"

모든 검색은 grep에 의해 수행됩니다. 물론 다른 매개변수에서 파일을 선택해야 하는 경우 find 가 올바른 솔루션입니다.

 find . -iname "*.php" -execdir grep -l "mystring" {} +

execdir 옵션은 각 디렉토리마다 각 grep 명령을 빌드하고 파일 이름을 하나의 명령( + )으로 연결합니다.


user2350426

경로가 있는 파일 이름을 가져오기 위한 내 명령 제안

 sudo find /home -name *.php

내 Linux OS에서 이 명령의 출력:

작성-샘플-3/html/mail/contact_me.php


apl

출처 : http:www.stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-on-linux

반응형