본문으로 바로가기

Find & Grep 명령어

category SECURITY/Linux 2017. 4. 4. 21:20


Find & Grep 명령어


1. find 명령어 ( find --help  man find )

 - 특정 파일을 찾고자 하는 경우에 사용 ( 디렉터리도 파일이기때문에 find 명령으로 찾을 수 있다. )

 - find [path] [expression]

 1) 파일의 이름으로 검색 : -name

  - find 경로 -name "파일의 이름" -print 

    리눅스에서는 -print액션이 생략되어있습니다. 경로는 하위 디렉터리도 포함한다.

    EX)

    ① find / -name "root" -print     : 정확히 root 이름인 것만 찾는다.

    ② find / -name "root*" -print    : root로 시작된 파일을 찾는다.

    ③ find / -name "*root" -print    : root로 끝나는 파일을 찾는다.

    ④ find / -name "*root*" -print   : root가 포함되있는 파일을 찾는다.

    ⑤ find / ! -name "*root*" -print : root가 포함되지 않는 파일을 찾는다.


 2) 파일의 타입으로 검색 : -type

   find / -type d -ls    : 디렉터리인 파일의 속성까지 출력

   b : block     c : character     d : directory 

   p : named pipe     f :regular file     l : symbolic     s : socket


 3) 특정 파일타입의 파일 이름을 가진 파일 검색 : and or ,

   find / -name "*root*" -type d -ls     : and 

   find / -name "*root*" -a -type d -ls

   find / -name "*root*" -and -type d -ls


   find / -name "*root*" -ls -o -type d -ls  : or

   find / -name "*root*" -ls -or -type d -ls

   

   find / -name "*root*" -ls , -type d -ls  : ,


   -o 와 ,의 차이

   find / -name "rc" -print -o -name "rc" -print

   find / -name "rc" -print , -name "rc" -print


 4) 액션 : -exec -ok

   touch file1 file2 file3 file4 file5

   find . -name "file*" -type f -exec ls {} \;

   find . -name "file*" -type f -exec mv {} /tmp/ \;

   mv ./file1 /tmp/

   mv ./file2 /tmp/

   mv ./file3 /tmp/

   mv ./file4 /tmp/

   mv ./file5 /tmp/

   find . -name "file*" -type f -ok mv {} /tmp/ \;    : 확인시켜준다.

   < mv ... ./file2 > ? y

   < mv ... ./file5 > ? y  

   < mv ... ./file3 > ? y

   < mv ... ./file4 > ? y

   < mv ... ./file1 > ? y


 5) 파일의 소유자(그룹) 검색 : -user -group

   find / -user root -ls  : 소유자가 root인 파일을 찾아준다.

   find / -group user1 -user user1 -ls  : 그룹소유자가 user1이고 소유자가 user1인 파일을 찾아준다.


 6) 파일의 시간으로 검색 : -mtime -ctime -atime

 - 일을 기준으로 변경시간, 수정시간, 접근시간

 - mtime +day or day or -day ( + : 초과, - : 미만 )

 - ctime +day or day or -day

 - atime +day or day or -day

   find / -atime 1 -exec stat {} \;

ex) 1 : 1일 전(과거 당일)

    -1 : 1일 이내(과거)

   +1 : 1일 이후(과거)


 7) 파일의 권한으로 검색 : -perm

 find / -perm 111 -ls : 권한이 111인 파일을 찾는다.

 find / -perm -111 -ls : 실행권한이 포함되어있는 파일을 찾는다.( - : 포함 )

 find / -user root -perm -4000 -ls : 소유자가 root이고 setuid가 설정되어있는 파일을 찾는다.


 8) 파일의 크기로 검색 : -size

 - size +크기 or 크기 or -크기

 - 1은 검색이 안된다

   find / -size 10M -exec ls -lh {} \;



2. grep 명령어(grep egrep)

 - 정규표현식 ( regular expression)

 - grep 명령에서만 쓰이지 않는다.

 - grep 명령은 확장된 정규 표현식을 지원하지 않는다.

 - -E 혹은 -P 옵션과 함께 사용

 - egrep 명령을 사용

 grep -E 'root' /etc/passwd   : 매칭되는 라인이 출력된다. 


 1) 정규 표현식

  .   : 임의의 한 문자      cat - | grep -E '....'       : 연속으로 한 문자가 네개가 있으면 그 문장을 출력한다. 

  ^  : 라인의 시작 문자   cat - | grep -E '^a'    : a로 시작되는 문장 출력

  $  : 라인의 끝 문자      cat - | grep -E 'root$' : root로 끝나는 문장 출력

  [ ] : 한개의 지정 문자   cat - | grep -E '[1234]' : []안에 있는 문자 중 1개가 있으면 그 문장을 출력

           - [1234][5678][0129][a-z][0-9][A-Z][a-zA-Z0-9]          [^] - : 브라켓 안쪽에 들어가게 되면 not의 의미(부정)


  반복

  ?   : 0, 1                  cat - | grep -E 'abcd?abcd'   cat - | grep -E 'abcd[1234]?abcd'

  *   : 0, 1번이상          cat - | grep -E 'abcd*abcd'   cat - | grep --color -E 'abcd*abcd'

  +  : 1번이상             cat - | grep -E 'abcd+abcd'

  {n} : n번

  {n,m} : n번부터 m번까지

  {n, } : n번이상

  { ,m} : 최대m번         cat - | grep -E 'abcd{0,4}abcd'

  ( ) : 문자열               cat - | grep -E 'a(abcd)?abcd'  ( )는 문자열


grep 참고 : http://www.nextree.co.kr/p4327/



'SECURITY > Linux' 카테고리의 다른 글

아카이브(tar), 압축(gzip, bzip, xzip)  (0) 2017.04.03
마운트(Mount), 런 레벨(Run Level)  (0) 2017.04.03
장치 - 디스크  (0) 2017.03.31
쉘에 대한 이해4  (0) 2017.03.30
쉘에 대한 이해3  (0) 2017.03.29