어쩌면 조금 이상 할 수도 있습니다. 어쩌면 다른 도구가있을 수도 있습니다.
다음 클래식 bash 명령을 사용하여 문자열이 포함 된 모든 파일을 찾습니다.
find . -type f | xargs grep "something"
여러 깊이에 많은 파일이 있습니다. “무언가”의 첫 번째 항목으로 충분하지만 찾기는 계속 검색하며 나머지 파일을 완료하는 데 시간이 오래 걸립니다. 내가하고 싶은 것은 grep back에서 “feedback”과 같은 것으로 찾기가 더 많은 파일 검색을 중지 할 수 있도록합니다. 그런 일이 가능합니까?
답변
찾기 영역 내에서 유지하십시오.
find . -type f -exec grep "something" {} \; -quit
이것이 작동하는 방식입니다.
이 -exec
(가) 때 작동합니다 -type f
true가됩니다. 그리고 일치하는 경우 grep
반환 0
(성공 / 참) 때문에 트리거됩니다.-exec grep "something"
-quit
답변
find -type f | xargs grep e | head -1
않는 정확히 다음 때 head
종료가 배관의 중간 요소가 생겼 파이프 ‘신호 통지, 종료 차례로 통지한다 find
. 다음과 같은 통지가 표시되어야합니다.
xargs: grep: terminated by signal 13
이것은 이것을 확인합니다.
답변
도구를 변경하지 않고이 작업을 수행하려면 (xargs를 좋아합니다)
#!/bin/bash
find . -type f |
# xargs -n20 -P20: use 10 parallel processes to grep files in batches of 20
# grep -m1: show just on match per file
# grep --line-buffered: multiple matches from independent grep processes
# will not be interleaved
xargs -P10 -n20 grep -m1 --line-buffered "$1" 2> >(
# Error output (stderr) is redirected to this command.
# We ignore this particular error, and send any others back to stderr.
grep -v '^xargs: .*: terminated by signal 13$' >&2
) |
# Little known fact: all `head` does is send signal 13 after n lines.
head -n 1