·터미널 명령어

grep

텍스트 처리

쓰는 꼴

grep -rn '[pattern]' [path]

글에서 무늬에 맞는 줄을 골라냅니다. 기본이 기초 정규식이라 + ? | 와 () 는 -E를 붙이거나 역슬래시를 씌워야 뜻이 삽니다.

자주 쓰는 옵션

옵션
-iIgnore case
-rSearch a whole directory tree; -R also follows symlinks
-nPrint the line number of each hit
-vInvert: print the lines that do not match
-lPrint only the names of the files that matched
-wMatch whole words only
-EExtended regex, so + ? | and () work unescaped
-CPrint N lines of context around each hit

예시

grep -rn 'TODO' src/

Every TODO in the tree, with file and line.

grep -v '^#' nginx.conf

Drops the comment lines.

ps aux | grep '[n]ginx'

Finds nginx without matching the grep itself.

파이프로 이어 쓰는 것이 본래 용도입니다. 한 명령이 다 하는 것보다 셋을 잇는 편이 짧습니다.

읽는 방법

  • 대괄호 [ ]는 넣어도 되고 안 넣어도 되는 자리입니다.
  • 점 셋 …은 여러 개를 이어 쓸 수 있다는 뜻입니다.
  • 옵션은 대소문자를 가립니다 — -r과 -R이 다른 명령도 있습니다.

자주 묻는 것

Q. grep은 무엇을 하나요?

글에서 무늬에 맞는 줄을 골라냅니다. 기본이 기초 정규식이라 + ? | 와 () 는 -E를 붙이거나 역슬래시를 씌워야 뜻이 삽니다.

Q. 어떻게 치나요?

grep -rn '[pattern]' [path] — 대괄호는 생략할 수 있는 자리입니다.

Q. 옵션은 몇 개나 자주 쓰나요?

여기 정리한 것은 8개입니다. 전체 목록은 man grep에 있습니다. 이 명령은 텍스트 처리 갈래입니다.

같이 보는 명령

man 페이지: man grep