programing tip

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

itbloger 2020. 9. 28. 08:24
반응형

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


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

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

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

어떻게 파일 이름 (경로 포함)을 얻을 수 있지만 일치하지 않습니까? 사용해야 xargs합니까? grepman 페이지 에서이 작업을 수행하는 방법을 보지 못했습니다 .


표준 옵션 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이 경우 에도 필요하지 않습니다 .


로부터 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.)

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

grep -rl "mystring"

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

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

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

참고 URL : https://stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-on-linux

반응형