Linux에서 일반적으로 사용되는 명령 | grep 명령 세부 정보 + 예제

grep 명령은 일상적인 터미널 작업이나 프로그래밍에 사용되는 Linux에서 자주 사용되는 명령으로 다음은 예제와 함께 소개합니다.

1. 기본 문법

grep [옵션] PATTERN [파일 ...]

공통 매개 변수 :

-i : 대소 문자 무시 (-i는 많은 명령에서이 의미를가집니다)

-v, --invert-match : 반전 표시, 일치하는 텍스트를 포함하지 않는 모든 행을 표시합니다.

-R, -r, --recursive : 각 디렉토리의 모든 파일을 재귀 적으로 읽습니다. -d recurse 옵션과 동일합니다.

-o, --only-matching : 일치하는 행에서 PATTERN과 일치하는 부분 만 표시합니다.

-n, --line-number : 각 출력 행 앞에있는 파일에 행 번호를 추가합니다.

--color : 일치하는 콘텐츠를 강조 표시합니다.이 매개 변수는 일반적으로 기본적으로 grep에 추가됩니다.

-A NUM, --after-context = NUM ​​: 일치하는 줄 바로 뒤에 다음 NUM 줄을 인쇄합니다.

-B NUM, --before-context = NUM ​​: 일치하는 행 앞에 위의 NUM 행을 인쇄합니다.

-C NUM, --context = NUM ​​: 일치하는 행의 컨텍스트 앞뒤에 NUM 행을 인쇄합니다.

2. 예

2.1 매개 변수 없음

현재 디렉토리에서 아래와 같이 "hosts"문자열이 포함 된 파일을 찾습니다.

[root@localhost ssh]# grep "hosts" ./*
./ssh_config:#   RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#

 검색 결과에서 파일이 먼저 나열되고 "hosts"문자열이있는 줄이 출력됩니다. 기본적으로 여기서 "호스트"와 같이 일치하는 콘텐츠가 강조 표시됩니다.

아래와 같이 시스템에서 명령의 별칭을 확인합니다.

[root@localhost ssh]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ssh]#

그 중 grep 명령은 grep --color = auto와 같으며 기본적으로 --color 매개 변수가 추가되었습니다.

2.2 -r 매개 변수

현재 디렉토리에서 "hosts"문자열이 포함 된 파일을 재귀 적으로 검색합니다. 즉, 현재 디렉토리와 현재 디렉토리 아래의 모든 디렉토리를 검색하고 재귀 적으로 검색합니다. 다음과 같이 : 

[root@localhost etc]# grep -r "hosts" ./*
./avahi/hosts:# See avahi.hosts(5) for more information on this configuration file!
./cupshelpers/preferreddrivers.xml:    <drivertype name="ghostscript">
./cupshelpers/preferreddrivers.xml:      <attribute name="ppd-product" match=".*Ghostscript"/>
./cupshelpers/preferreddrivers.xml:     <drivertype>ghostscript</drivertype>
./dnsmasq.conf:# from /etc/hosts or DHCP only.
./dnsmasq.conf:# If you don't want dnsmasq to read /etc/hosts, uncomment the
./dnsmasq.conf:#no-hosts
./dnsmasq.conf:# or if you want it to read another file, as well as /etc/hosts, use
./dnsmasq.conf:#addn-hosts=/etc/banner_add_hosts
./dnsmasq.conf:# automatically added to simple names in a hosts-file.
./dnsmasq.conf:#expand-hosts
……
./tcsd.conf:#  on this machine's TCSD by TSP's on non-local hosts (over the internet).
./yum/pluginconf.d/fastestmirror.conf:hostfilepath=timedhosts.txt
[root@localhost etc]#

2.3 -i 매개 변수

현재 디렉토리에서 "hosts"문자열이 포함 된 파일을 찾으십시오. 문자열의 문자는 아래와 같이 대소 문자를 구분하지 않습니다. 

[root@localhost ssh]# grep -i "hosts" ./*
./ssh_config:# HOSTS tmp
./ssh_config:#   RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#

위의 검색 결과에서 HOSTS, 호스트 및 호스트가 모두 발견되었습니다.

2.4 -o 매개 변수

현재 디렉토리에서 "hosts"문자열이 포함 된 파일을 찾고 "hosts"와 동일한 내용 만 표시합니다. 다음과 같이 :

[root@localhost ssh]# grep -o "hosts" ./*
./ssh_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
[root@localhost ssh]#

그 중에서 표시되는 일치하는 콘텐츠는 "호스트"와 동일한 콘텐츠 만 표시합니다.

2.5 -n 매개 변수

현재 디렉토리에서 "hosts"문자열이 포함 된 파일을 찾아 아래와 같이 일치하는 콘텐츠의 줄 번호를 표시합니다.

[root@localhost ssh]# grep -n "hosts" ./*
./ssh_config:24:#   RhostsRSAAuthentication no
./sshd_config:54:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:56:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:59:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:60:#IgnoreRhosts yes
[root@localhost ssh]# 

일치 전에 줄 번호가 추가됩니다. 

2.6 -A, -B, -C 매개 변수

-A : 아래와 같이 일치하는 줄 바로 뒤에 다음 한 줄을 인쇄합니다.

[root@localhost ssh]# grep -A 1 "hosts" ./*
./ssh_config:#   RhostsRSAAuthentication no
./ssh_config-#   RSAAuthentication yes
--
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
--
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
[root@localhost ssh]#

이 중 "-"는 인접한 일치 그룹 사이에-줄이 인쇄됨을 의미하며, 실제 파일에서 일치하는 두 그룹이 밀접하게 연결되어 있으면 "-"로 구분되지 않습니다.

-B : 아래와 같이 일치하는 줄 앞에 위의 두 줄을 인쇄합니다.

[root@localhost ssh]# grep -B 2 "hosts" ./*
./ssh_config-#   ForwardAgent no
./ssh_config-#   ForwardX11 no
./ssh_config:#   RhostsRSAAuthentication no
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#

-C : 아래와 같이 일치하는 줄의 전후에 두 줄을 인쇄합니다.

[root@localhost ssh]# grep -C 2 "hosts" ./*
./ssh_config-#   ForwardAgent no
./ssh_config-#   ForwardX11 no
./ssh_config:#   RhostsRSAAuthentication no
./ssh_config-#   RSAAuthentication yes
./ssh_config-#   PasswordAuthentication yes
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
./sshd_config-# To disable tunneled clear text passwords, change to no here!
[root@localhost ssh]#

셋, 요약

grep은 Linux 터미널 작업에서 자주 사용되는 명령으로, 일반적으로 지정된 내용이 포함 된 파일을 찾는 데 사용됩니다.

참조 :

[1] Linux grep 매뉴얼;

추천

출처blog.csdn.net/u011074149/article/details/113742875