리눅스 명령어 grep은 작업 중에 많은 파일과 텍스트 데이터를 다룰 때 필요한 정보를 빠르게 찾아내는 데 중요한 역할을 합니다. 이번 포스팅에서는 grep
명령어의 기본 사용법과 다양한 옵션들에 대해 알아보겠습니다.
목차
리눅스 명령어 grep란?
grep
은 “global regular expression print”의 약자로, 텍스트 데이터에서 특정 패턴과 일치하는 라인을 찾고 출력해주는 명령어입니다. 파일의 내용이나 텍스트 출력물 중 원하는 단어 또는 문자열을 검색할 때 유용하게 사용됩니다.
기본 사용법
grep
명령어의 기본 형식은 다음과 같습니다.
grep [옵션] 검색할_패턴 파일명
ShellScript앞으로 예제에서 사용할 example.txt의 파일 내용은 아래와 같습니다.
Hello, how are you doing today?
It's a pleasure to meet you.
Nice to meet you as well.
HELLO, this is Charles speaking.
Can I assist you with anything?
Do you need any help?
hello, may I ask you a question?
Could you please help me with this?
hello, I'm calling regarding the job posting.
I'd like to make a reservation.
Plaintext가장 기본적인 예제를 살펴보겠습니다. 예를 들어, example.txt
파일에서 “hello”라는 단어를 검색하려면 다음과 같이 입력합니다.
grep "hello" example.txt
ShellScript이 명령어는 다음과 같이 example.txt
파일에서 “hello”라는 단어가 포함된 모든 라인을 출력합니다.

그리고 다음은 “this”가 들어간 라인을 모두 출력한 결과입니다.

주요 옵션들
grep
명령어에는 다양한 옵션들이 있어서 검색 결과를 좀 더 세밀하게 제어할 수 있습니다. 여기서는 몇 가지 중요한 옵션들을 소개합니다.
-i(ignore case): 대소문자 구분 없이 검색
기본적으로 grep
은 대소문자를 구분하여 검색합니다. 하지만 -i
옵션을 사용하면 대소문자를 구분하지 않고 검색할 수 있습니다.
grep -i "hello" example.txt
ShellScript이 명령어는 “Hello”, “HELLO” 등 대소문자 조합과 상관없이 “hello”를 포함하는 모든 라인을 출력합니다.

-v(invert match): 일치하지 않는 라인 출력
-v
옵션은 검색 패턴과 일치하지 않는 라인을 출력합니다.
grep -v "hello" example.txt
ShellScript이 명령어는 “hello”가 포함되지 않은 모든 라인을 출력합니다. 아래에서는 i 옵션을 함께 사용해서 대소문자 구분없이 “hello”가 포함되지 않은 라인들을 출력했습니다.

-r(recursive): 디렉토리 내의 파일들 검색
-r
옵션은 지정한 디렉토리와 그 하위 디렉토리의 모든 파일을 검색합니다.
grep -r "hello" /path/to/directory
ShellScript이 명령어는 /path/to/directory
디렉토리 내의 모든 파일에서 “hello”를 검색합니다. 아래 그림은 현재 디렉토리 하위에 있는 파일 중 hello 문자열이 들어간 라인을 출력하게 했습니다. 제일 앞에는 어떤 파일에 있는 내용인지 알 수 있도록 파일 이름이 함께 출력됩니다.

-l(files with matches): 패턴이 포함된 파일명 출력
-l
옵션은 패턴이 포함된 파일의 이름만을 출력합니다.
grep -l "hello" *.txt
ShellScript이 명령어는 현재 디렉토리의 모든 .txt
파일 중 “hello”가 포함된 파일의 이름을 출력합니다.

-n(line number): 일치하는 라인의 번호 출력
-n
옵션을 사용하면 검색 패턴과 일치하는 라인의 번호를 함께 출력합니다.
grep -n "hello" example.txt
ShellScript이 명령어는 “hello”가 포함된 라인의 번호와 함께 출력합니다.

-c(count): 일치하는 라인의 수 출력
-c
옵션은 패턴과 일치하는 라인의 수를 출력합니다.
grep -c "hello" example.txt
ShellScript이 명령어는 example.txt
파일에서 “hello”가 포함된 라인의 수를 출력합니다.

-A(after context), -B(before context), -C(context): 컨텍스트 라인 출력
-A
옵션은 일치하는 라인 이후의 N개 라인을, -B
옵션은 이전의 N개 라인을, -C
옵션은 양쪽의 N개 라인을 함께 출력합니다.
grep -A 2 "hello" example.txt
ShellScript이 명령어는 “hello”가 포함된 라인과 그 이후의 두 개 라인을 출력합니다. 이후에 2개의 라인이 존재하지 않는 경우에는 아래 그림처럼 표현할 수 있는 만큼만 출력됩니다.

다음은 -B 옵션을 이용해서 “hello”가 포함된 라인과 그 이전 두 개 라인을 출력합니다.

이제 마지막으로 -C 옵션을 이용해서 “hello”가 포함된 라인과 그 이전 두 개 라인과 이후 두 개 라인을 모두 출력합니다.

유용한 활용 방법
로그 파일 분석
시스템 로그 파일에서 특정 에러 메시지를 검색할 때 유용합니다. 예를 들어, /var/log/syslog
파일에서 “error” 메시지를 찾으려면 다음과 같이 입력합니다.
grep "error" /var/log/syslog
ShellScript소스 코드 검색
소스 코드 파일에서 특정 함수 호출을 검색할 때 사용할 수 있습니다. 예를 들어, 현재 디렉토리의 모든 .c
파일에서 main
함수를 검색하려면 다음과 같이 입력합니다.
grep -n "main" *.c
ShellScript디렉토리 내 파일 내용 검색
특정 디렉토리 내의 모든 파일에서 검색할 때 유용합니다. 예를 들어, /etc
디렉토리 내의 모든 파일에서 “network”라는 단어를 검색하려면 다음과 같이 입력합니다.
grep -r "network" /etc
ShellScript정리
이번 포스팅에서는 grep
명령어의 기본 사용법과 다양한 옵션들에 대해 알아보았습니다. grep
은 파일과 텍스트 데이터를 효율적으로 검색할 수 있는 강력한 도구로, 로그 파일 분석, 소스 코드 검색, 디렉토리 내 파일 내용 검색 등 다양한 용도로 활용할 수 있습니다. 여러 옵션을 적절히 활용하여 자신에게 맞는 최적의 검색 결과를 얻으시길 바랍니다. grep
을 잘 활용하면 리눅스에서의 작업 효율성이 크게 향상될 것입니다.