Dev
find 명령어 사용하기
김문기
2016. 7. 22. 15:53
GUI 로 파일 또는 파일 속의 내용을 검색 하여도 되지만
find 명령어를 사용하면 빠르게 원하는 파일 혹은 내용을 찾을 수 있다.
1. 파일 검색
1.1 # find [경로] -name "[파일 이름]"
경로부터 하위경로까지 검색하여 매칭되는 파일이름을 보여준다.
"*"을 적절히 집어넣어 파일명이 생각나지 않을때도 찾아 낼 수 있다.
1.1.1 실행 테스트
[root@localhost ~]# find / -name "main.c"
/root/main.c
[root@localhost ~]# find / -name "main*.c"
/usr/src/main_test.c
/root/main.c
1.2 # find [경로]
하위 경로 파일 전부 검색하기 (파일이름이 생각 안날 때)
경로에 . 을 사용하여 상대경로로 검색할 수 있다.
1.2.1 실행 테스트
[root@localhost ~]# find .../.cshrc./anaconda-ks.cfg./.bash_history./.Xauthority./.bashrc./test./test/test1./test/test2./main.c./.tcshrc./.bash_logout./.rnd./.bash_profile./.viminfo./install.log.syslog./install.log
2. 파일 속 내용 검색
2.1 # find [경로] -name "[파일 이름]" | xargs grep "[내용]"
경로부터 하위경로까지 검색하여 매칭되는 파일이름과 매칭 내용을 보여준다.
2.1.1 실행 테스트
[root@localhost ~]# find . -name "test*" | xargs grep "main()"./test/test_main.c:main(){[root@localhost ~]# find | xargs grep "main()"./test/test_main.c:main(){
2.2 # find [경로] -name "[파일 이름]" | xargs grep -I "[내용]"
바이너리 파일때문에 원하지 않는 결과 값이 나올 때는 grep 옵션에 -I (대문자 아이) 를 주어서 명령어를 실행시켜보자.