2. 검색

검색 명령어

find

  • 조건에 맞는 파일을 찾아 명령을 수행

  • 기본 사용 : find [options] path EXPR

주로 쓰는 옵션

  • -name : 이름으로 검색

  • -regex : 정규식 매치로 검색

  • -empty : 빈 디레터리 혹인 빈 파일 검색

  • -size : 사이즈로 검색(M,G로 표기 가능)

    • -N : 이하

    • +N : 이상

  • -type : 파일 타입으로 검색

    • d : 디렉터리

    • p : named pipe

    • f : regular file

    • l : softlink

    • s : socket

  • -perm : 퍼미션으로 검색

    • mode : 정확히 일치하는 파일

    • +mode : 모든 flag가 포함된 파일

    • /mode : 어떤 flag라도 포함된 파일

  • -delete : 파일삭제. 위험. 삭용 자제

  • -ls : ls -dils 명령 수행

  • -print : 파일 이름 출력

  • -printf : 파일 이름을 포맷에 맞게 출력

  • -exec : 주어진 명령 수행

  • -execdir : 해당 디렉터리로 이동하여 명령 수행

  • -ok : 사용자에게 확인 후 exec

  • -okdir : 사용자에게 확인 후 실행 execdir

# 현재 디렉터리에서 검색 시작
$ find . 

# etc 디렉터리 이하 검색
$ find /etc

# 파일 + 디렉터리 수
$ find . | wc -l

# 파일명이 event포함인것 찾기
$ find . | grep event

# 이름이 .py로 끝나는 모든 파일 탐색
$ find . -name "*.py"

# 절대 경로 기반 현재 위치에서 탐색, . 은 현재 디렉터리 상대경로 기준
$ find `pwd` -name "*.py"

# 정규식 이용
$ find `pwd` -regextype egrep -regex '.*hash.*.c$'

$ find . -empty     # 빈 파일 or 디렉터리
$ find . -type f    # 파일 타입
$ find . -type d    # 디렉터리 타입
$ find . -type l    # 링크 타입

# 권한으로 찾기
$ find . -perm 777
$ find . -perm 0644 | wc -l
$ find . -perm /u+x | wc -l
$ find . -perm /u+x -ls         # 유저에 실행권한 있는거 찾기

$ find . -name "*.py" -exec ls \;
$ find . -name "*.py" -exec stat {} \;
$ find . -name "*.py" -execdir stat {} \;

$ find . -name "*.py" -ok stat {} \;

$ find . -name "*.py" -ok rm -f {} \;
$ find . -name "*.py" -okdir rm -f {} \;

grep

  • 파일 내용 중 원하는 내용을 찾음

  • grep [options] PATTERN [FILE...]

자주 쓰는 옵션

  • -r : recursive

  • -i : ignore case

  • -v : invert match

  • -q : quit mode

$ grep stdio *.c

$ grep ftok *.c | awk -F: '{ print $1 }' | sort -u

$ grep ftok *.c -q

$ grep "\<for\>" *.c

$ grep "int" *.c
$ grep "^int" *.c
$ grep "^static" *.c
$ grep "^static.*(void)$" *.c

apropos

  • man page 이름과 설명을 검색한다

자주 쓰는 옵션

  • -s, --sections=LIST, --section=LIST : 탐색할 섹션을 콜론으로 구분하여 입력

$ apropos print
$ apropos print -s 3
$ apropos pthread

locate

  • 파일의 위치를 찾음

  • updatedb가 저장해놓은 DB 파일 내에서 검색하므로 누락파일이 발생할 수도 있음

  • locate [OPTIONS]... PATTERN...

자주 쓰는 옵션

  • -i, --ignore-case : 대소문자 구분없이 검색

  • -l, --limit, -n LIMIT : 출력결과를 LIMIT만큼만 출력

  • --regex : 패턴을 정규식으로 해석

$ locate asdadasdas
$ locate main.c
$ locate  --regex "\/main.c$"
$ locate  --regex "\/main.c$" -n 3

which

  • 실행 파일의 위치를 보여줌

자주 쓰는 옵션

  • 없음

$ which java
$ which ls
$ which ethtool
$ which ifconfig

Last updated