LInux 1

13. 검색기능 정리 LInux

양곰3 2017. 4. 11. 09:44

find & grep -> 검색기능을 가지고 있다.


1. find : 특정 파일 검색

#>find <path> <option> ...


-name <value>

#>find /etc -name "passwd"


-print( 기본값 ) : 기본값으로 설정되어있어 

  입력할 필요는없다.

ex)

#>find /etc -name "passwd" -print

-ls : 자세히 출력

ex)

#>find /etc -name "passwd" -ls

-delete : 검색한 내용 삭제

ex)

#>find /etc -name "pass" -delete

-empty : 비어있는파일 보여줌

ex)

#>find /etc -name "passwd" -empty


-type <value> : 해당 타입인 파일을 찾아줌

-type d 디렉터리

-type f 일반파일

-type l 링크파일

-type b 장치파일

#>find /root -name "test" -type d

#>find /root -name "test" -type f

ex)

#>mkdir /root/test

#>touch /root/test/test


#>find /root -name "test"



-exec <command> {} \;

#>find /root -name "test" -type f -exec mv 찾은파일 /tmp \;                                                       --> {}

ex)

#>find /tmp -name "test" -type f -exec rm -rf {} \;


-size n ( +n | n | -n )

#>find /etc -size 2536c

#>find /etc -size +2536c

#>find /etc -size -2536c

c는 byte 단위


-user <user_name>

-group <group_name>

#>find /home -user linux01

#>find /home -group linux

#>find /home -user linux01 -group linux


-perm <octal> -> -<octal> 해당권한을 포함하는 모든것을 찾는다. 

#>find /home -perm 700

#>find /home -perm -100 -> 100 권한을 포함하고 있는 모든것


#>stat file1

-atime n : n일 이전에 접근한( 열어본 ) 파일

-mtime n : n일 이전에 내용이 수정된 파일

-ctime n : n일 이전에 속성이 수정된 파일


-inum n

#>find /root -inum 72013813


2. grep : 파일에서 내용으로 검색

#>cp /etc/passwd ~/

-r <value>

#>grep -r "root" ~/passwd

-c <value> : 찾고자하는 문자가 들어간 

  라인수가 나옴

#>grep -c "root" ~/passwd


-i <value> : 대소문자 구분 안하겠다는 옵션

#>grep -i "ROOT" ~/passwd


-n <value> : 줄번호와 같이 출력

#>grep -n "root" ~/passwd


-v <value> : 해당문자 라인이 들어가있지 

  않은것만 출력

#>grep -v "root" ~/passwd


3. 정규표현식

#>grep -E


^<value> 시작문자

#>grep -E "^root" ~/passwd -> 시작문자가 root인 

 라인출력 


<value>$ 끝문자

#>grep -E "bash$" ~/passwd

-> 끝문자가 bash인 라인 출력


[값값값] 문자 집합 -> 문자집합중에 하나라도 들어가

#>grep -E [root] ~/passwd 있으면 출력


[^값값값] not -> 반대

#>grep -E [^rot] ~/passwd


#>grep -E [a-g] ~/passwd -> a부터g까지의 

문자를 찾음


+ 1번이상

* 0번이상

? 0 or 1


#>grep -E [a-z][a-z][a-z]....

=> #>grep -E [a-z]+ ~/passwd

#>grep -E [a-z]+[0] ~/passwd -> 소문자로 시작해서                                                     0으로 끝남

#>grep -E [a-z]*[0] ~/passwd



ex) 소문자로 시작하고(소문자로만 구성) 끝문자가 0인 단어     를 찾으세요

[a-z]+[0]

#>grep -E [a-z]+[0] /root/passwd

#>grep -E [a-z][a-zA-z0-9]*[0]


+ 1번이상

* 0번이상

? 0 or 1

{num1} num1번 반복

{num1,num2} num1이상 num2이하


(문자열) 문자열 검색

| or (또는)


#>wget estrellita.org/ex3.txt


ex1) .com 이메일 주소 검색

[a-zA-Z0-9]+[@][a-zA-Z0-9]+[.][c][o][m]

[a-zA-Z0-9]+[@][a-zA-Z0-9]+(.com)

#>grep -E [a-zA-Z0-9]+[@][a-zA-Z0-9]+[.][c][o][m] /root/ex3.txt

#>grep -E [a-zA-Z0-9]+[@][a-zA-Z0-9]+(.com) /root/ex3.txt

#>grep -E [a-zA-Z0-9]+[@][a-zA-Z0-9]+(.com)?(.net)? /root/ex3.txt

#>grep -E [a-zA-Z0-9]+[@][a-zA-Z0-9]+(.com|.net) /root/ex3.txt

-> 아이디는6글자에서 10글자 사이

'[a-zA-Z0-9]{6,10}[@][a-zA-Z0-9]+(.com|.net|.co.kr)'

#>grep -E '[a-zA-Z0-9]{6,10}[@][a-zA-Z0-9]+(.com|.net|.co.kr)' /root/ex3.txt

'.{6,10}[@][a-zA-Z0-9]+(.com|.net|.co.kr)'

--> '.'은 모든 문자를 뜻함