본문 바로가기
리눅스

grep

by slowcloud_ 2025. 8. 24.

주어진 입력으로부터, 인자로 받은 문자열이 존재하는지 찾는 명령어

 

기본 사용방법은 다음과 같다:

grep [OPTION]... patterns [FILE]...

 

아래와 같이 사용하면, test1.txt와 test2.txt에서 test 문자열을 포함하는 줄을 출력한다.

grep test test1.txt test2.txt

 

기본적으로 정규표현식을 사용한다. 다음 명령어는 test 문자열로 시작하는 줄을 출력한다.

grep ^test test1.txt

 

다음과 같은 옵션이 존재한다.

더 많은 옵션은 --help 명령어로 확인할 수 있다.

 

-c, --count

결과의 개수를 반환한다.

 

--color[=WHEN], --colour[=WHEN]

결과에 색을 입힌다. 'always', 'never', 'auto' 중 하나를 쓰면 된다.

bash를 사용하고 있다면, 다음과 같이 alias가 설정되어 있다.

alias grep='grep --color=auto'

 

-i, --ignore-case

대소문자를 구별하지 않는다.

 

-r, --recursive

디렉터리와 파일을 순회하며 패턴과 일치하는 문자열을 포함하는 줄을 모두 출력한다.

 

-v, --invert-match

일치하는 문자열을 제외하고 모두 출력한다.

 

 

ripgrep과 같이 recursive grep을 구현한 오픈소스 명령어도 있다. nvim 등의 플러그인에서 자주 사용된다.

GNU grep -r 옵션보다 훨씬 빠르다고 한다.

https://github.com/BurntSushi/ripgrep

 

GitHub - BurntSushi/ripgrep: ripgrep recursively searches directories for a regex pattern while respecting your gitignore

ripgrep recursively searches directories for a regex pattern while respecting your gitignore - BurntSushi/ripgrep

github.com

 

'리눅스' 카테고리의 다른 글

커널스페이스와 유저스페이스  (0) 2026.03.26
cmake 기본 활용  (0) 2026.03.06
fzf 소개 및 간략한 사용법  (1) 2025.07.17