grep --exclude는 내 파일을 제외하지 않습니다.

grep --exclude는 내 파일을 제외하지 않습니다.

특정 문자열이 있는 디렉터리를 검색하고 있습니다(해당 문자열이 있는 위치와 파일의 모든 인스턴스를 확인하기 위해). 그런데 특정 파일을 검색에서 제외하고 싶습니다.

이게 무슨 일이야 -

$echo "searchstring" > ./old_folder/useless_file
$echo "searchstring" > ./new_folder/good_file
$grep -r --exclude="old_folder/useless_file" searchstring *
./old_folder/useless_file:searchstring
./new_folder/good_file:searchstring

이것이 내가 원하는 결과입니다 -

./new_folder/good_file:searchstring

답변1

--exclude옵션은 디렉토리나 전체 경로가 아닌 파일 이름과 일치하는 glob을 사용합니다.

  --exclude=GLOB
          Skip   files  whose  base  name  matches  GLOB  (using  wildcard
          matching).  A file-name  glob  can  use  *,  ?,  and  [...]   as
          wildcards,  and  \  to  quote  a wildcard or backslash character
          literally.

따라서 다음과 같이 할 수 있습니다.

$ grep -r --exclude="*useless_file*" searchstring 
new_folder/good_file:searchstring

또는 디렉터리의 모든 파일을 제외합니다.

$ grep -r --exclude-dir="old_folder" searchstring 
new_folder/good_file:searchstring

관련 정보