
Linux 시스템에서 파일과 디렉토리를 효율적으로 찾는 다양한 방법을 소개합니다.
1. find - 강력한 파일 검색 명령어
가장 강력하고 유연한 파일 검색 도구입니다.
기본 사용법
find . -name "filename.txt"
find . -iname "filename.txt"
find /home/user -name "*.log"
|
파일 타입으로 찾기
find . -type f -name "*.txt"
find . -type d -name "backup"
find . -type l
|
파일 크기로 찾기
find . -type f -size +100M
find . -type f -size -10k
find . -type f -size 1G
|
수정 시간으로 찾기
find . -type f -mtime -7
find . -type f -mtime +30
find . -type f -mmin -1440
find . -type f -mmin -60
|
권한으로 찾기
find . -type f -perm 0644
find . -type f -perm /111
find / -type f -perm -4000 2>/dev/null
|
소유자로 찾기
find . -user username
find . -group groupname
|
찾은 파일에 명령 실행
find . -name "*.tmp" -type f -delete
find . -name "*.log" -type f -exec rm {} \;
find . -name "*.log" -type f -ok rm {} \;
find . -name "*.log" -type f -exec rm {} +
|
복합 조건
find . -name "*.txt" -size +1M
find . -name "*.txt" -o -name "*.log"
find . -type f -not -name "*.txt"
find . \( -name "*.txt" -o -name "*.log" \) -size +1M
|
실용적인 예시
find . -type f -empty
find . -type d -empty
find . -type f ! -name "*.git*"
find . -name ".*"
find . -type f -printf '%f\n' | sort | uniq -d
find . -type f -printf '%T+ %p\n' | sort -r | head -10
|
2. locate - 빠른 파일 검색
데이터베이스를 사용하여 매우 빠르게 파일을 찾습니다.
sudo apt install mlocate sudo yum install mlocate
sudo updatedb
locate filename.txt
locate -i filename.txt
locate -b '\filename.txt'
locate -l 10 "*.conf"
locate -S
|
3. grep - 파일 내용으로 검색
파일 내용을 검색하여 특정 텍스트를 포함한 파일을 찾습니다.
기본 사용법
grep "search_text" file.txt
grep "search_text" *.txt
grep -r "search_text" /path/to/dir
grep -i "search_text" file.txt
|
유용한 옵션
grep -l "search_text" *.txt
grep -n "search_text" file.txt
grep -v "search_text" file.txt
grep -w "word" file.txt
grep -A 3 -B 3 "search_text" file.txt grep -C 3 "search_text" file.txt
grep --color=auto "search_text" file.txt
|
정규 표현식
grep "^start" file.txt grep "end$" file.txt grep "test.*text" file.txt
grep -E "pattern1|pattern2" file.txt grep -E "[0-9]{3}-[0-9]{4}" file.txt
grep -P "\d{3}-\d{4}" file.txt
|
실용 예시
grep -r "ERROR" /var/log/
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file.txt
grep -r --include="*.py" "def function_name" .
grep -r --exclude="*.min.js" "search_text" .
grep -r --exclude-dir=node_modules "search_text" .
|
4. which - 실행 파일 경로 찾기
PATH에 있는 실행 파일의 위치를 찾습니다.
which python which java which node
which -a python
|
5. whereis - 바이너리, 소스, 매뉴얼 찾기
whereis python
whereis -b python
whereis -m python
whereis -s python
|
6. type - 명령어 타입 확인
type ls type cd type python
type -a python
|
7. fd - 현대적인 find 대안
더 빠르고 사용하기 쉬운 find의 대안입니다.
sudo apt install fd-find brew install fd
fd pattern
fd -t f pattern fd -t d pattern
fd -H pattern
fd -p path/pattern
fd -e txt fd -e log -e txt
fd -E node_modules pattern
|
8. ripgrep (rg) - 빠른 grep 대안
매우 빠른 코드 검색 도구입니다.
sudo apt install ripgrep brew install ripgrep
rg "search_text"
rg -t py "def " rg -t js "function"
rg -i "search_text"
rg --hidden "search_text"
rg "search_text" -g '!node_modules'
rg -l "search_text"
rg --stats "search_text"
|
9. tree - 디렉토리 구조 시각화
sudo apt install tree
tree
tree -L 2
tree -d
tree -a
tree -P "*.txt"
tree -I "node_modules|.git"
tree -h
|
10. 실전 활용 스크립트
대용량 파일 찾기
#!/bin/bash
find . -type f -size +1G -exec ls -lh {} \; | awk '{print $5, $9}' | sort -hr
|
오래된 로그 파일 정리
#!/bin/bash
find /var/log -name "*.log" -type f -mtime +30 -delete
|
특정 텍스트를 포함한 파일 찾기
#!/bin/bash
grep -r "ERROR" /var/log --include="*.log" -l
|
중복 파일 찾기 (md5 해시 기반)
#!/bin/bash
find . -type f -exec md5sum {} \; | sort | uniq -w32 -dD
|
최근 수정된 파일 모니터링
#!/bin/bash
find /path/to/watch -type f -mmin -5
|
성능 비교
| 도구 |
속도 |
용도 |
설치 필요 |
find |
보통 |
범용적인 파일 검색 |
기본 설치 |
locate |
매우 빠름 |
파일명 검색 |
필요 |
grep |
보통 |
파일 내용 검색 |
기본 설치 |
fd |
빠름 |
현대적인 파일 검색 |
필요 |
rg |
매우 빠름 |
코드 검색 |
필요 |
정리
- 파일명으로 찾기:
find, locate, fd
- 파일 내용으로 찾기:
grep, rg
- 실행 파일 찾기:
which, whereis, type
- 대용량 검색:
locate (사전 인덱싱) 또는 fd, rg (빠른 성능)
- 정확한 제어:
find (가장 강력한 옵션)
일반적으로 **find**와 **grep**을 조합하면 대부분의 검색 작업을 수행할 수 있습니다. 성능이 중요하다면 **fd**와 **rg**를 설치하여 사용하는 것을 추천합니다.