본문 바로가기
Linux/Linux공통

find 명령어

by ccclog 2024. 6. 13.
반응형

find 명령어는 파일 및 디렉토리를 검색하는 데 사용되는 매우 강력한 도구입니다. 다양한 옵션을 통해 파일의 이름, 크기, 수정 시간, 유형 등을 기준으로 검색할 수 있습니다. 아래에 find 명령어의 주요 사용법과 옵션을 정리했습니다.

기본 문법

find [경로] [검색 조건] [연산]

주요 옵션 및 예제

  1. 특정 디렉토리에서 모든 파일 및 디렉토리 찾기
    find /path/to/directory
  2. 특정 이름의 파일 찾기
    find /home/user -name "test.txt"
    find /path/to/directory -name "filename"
  3. 대소문자를 구분하지 않고 이름 검색
    find /home/user -iname "test.txt"
    find /path/to/directory -iname "filename"
  4. 특정 확장자를 가진 파일 찾기
    find /home/user -name "*.jpg"
    find /path/to/directory -name "*.ext"
  5. 파일 유형별로 검색
    • 일반 파일
      find /path/to/directory -type f
    • 디렉토리
      find /path/to/directory -type d
  6. 특정 크기의 파일 찾기
    • +N: N보다 큰 파일
    • -N: N보다 작은 파일
    • Nc: N 바이트
    • Nw: N 워드 (2 바이트)
    • Nb: N 블록 (512 바이트)
    • Nk: N 킬로바이트
    • NM: N 메가바이트
    • NG: N 기가바이트

     
    find /home/user -size +100M
    find /path/to/directory -size [+|-]N[cwbkMG]
  7. 특정 시간 내에 수정된 파일 찾기
    • N일 이내에 수정된 파일
      find /path/to/directory -mtime -N
    • N일 전에 수정된 파일
      find /path/to/directory -mtime +N
    find /home/user -mtime -7 # 최근 7일 이내에 수정된 파일 find /home/user -mtime +30 # 30일 이전에 수정된 파일
  8. 특정 시간 내에 액세스된 파일 찾기
    • N일 이내에 액세스된 파일
      find /path/to/directory -atime -N
    • N일 전에 액세스된 파일
      find /path/to/directory -atime +N
     
    find /home/user -atime -7 # 최근 7일 이내에 액세스된 파일 find /home/user -atime +30 # 30일 이전에 액세스된 파일
  9. 특정 권한을 가진 파일 찾기
    find /home/user -perm 644
     
    find /path/to/directory -perm mode
  10. 특정 소유자 또는 그룹의 파일 찾기
    • 소유자
      find /path/to/directory -user username
    • 그룹
      find /path/to/directory -group groupname
      find /home/user -user alice find /home/user -group developers
  11. 다중 조건 사용
    • AND 조건
      find /path/to/directory -name "filename" -and -size +100M
    • OR 조건
      find /path/to/directory \( -name "file1" -o -name "file2" \)
    • NOT 조건
      find /path/to/directory ! -name "filename"
      find /home/user -name "*.log" -and -size +10M find /home/user \( -name "*.txt" -o -name "*.md" \) find /home/user ! -name "*.bak"

검색 결과에 대한 연산

  1. 파일 삭제
    find /home/user -name "*.tmp" -delete
    find /path/to/directory -name "filename" -delete
  2. 명령어 실행
    find /home/user -name "*.log" -exec rm {} \;
    find /path/to/directory -name "filename" -exec command {} \;
  3. 검색 결과 출력-print는 기본 옵션으로, 생략해도 결과가 출력됩니다.
    find /path/to/directory -name "filename" -print
  4. 명령어 대화형 실행
    find /home/user -name "*.log" -ok rm {} \;
    find /path/to/directory -name "filename" -ok command {} \;

이 외에도 find 명령어는 매우 강력하고 유연한 옵션들을 제공하므로, 특정 상황에 맞는 다양한 활용이 가능합니다. 필요에 따라 man find를 참조하여 더 많은 옵션과 예제를 확인할 수 있습니다.

반응형