'find'에 해당되는 글 1건

  1. 2009/01/21 Find 명령어 사용 예.
Linux2009/01/21 11:38
가끔 프로그래밍을 하거나. 문서를 찾을때 파일을 찾아야 되는 경우가 종종 생긴다.
이럴때  Find명령어를 이용한다면 효율적으로 파일을 쉽게 찾을수 있을것이다.

TinyOS파일을 찾던도중 Timer라는 단어가 들어간 파일을 찾고 싶어져 find라는 명령어의 기능에 대해 실습해보았다.

예제 1)

현재폴더와 하위폴더에 Timer라는 단어가 들어간 파일을 모두 찾아서 상세한 정보를 출력하게 하였다.

$ find . -name '*Timer*' -exec ls -al {} \;
-rw-r--r--    1 Administ 없음         2044 Oct  8  2003 ./apps/Blink/SingleTimer.nc
-rw-r--r--    1 Administ 없음         2044 Oct  8  2003 ./apps/BlinkTask/SingleTimer.nc
-rw-r--r--    1 Administ 없음         2766 Oct  8  2003 ./apps/HighFrequencySampling/MicroTimer.nc
-rw-r--r--    1 Administ 없음         3877 Oct  8  2003 ./apps/HighFrequencySampling/MicroTimerM.nc
-rw-r--r--    1 Administ 없음         2410 Jan 20 12:49 ./doc/nesdoc/cricket/apps.Blink.SingleTimer.
nc.html
-rw-r--r--    1 Administ 없음          715 Jan 20 12:49 ./doc/nesdoc/cricket/apps.Blink.SingleTimer.
nc.if.dot
-rwxr-xr-x    1 Administ 없음         2528 Jan 20 12:49 ./doc/nesdoc/cricket/apps.Blink.SingleTimer.
nc.if.gif
lrwxrwxrwx    1 Administ 없음          178 Jan 20 12:49 ./doc/nesdoc/cricket/apps.Blink.SingleTimer.
nc.source -> /opt/tinyos-1.x/apps/Blink/SingleTimer.nc
-rw-r--r--    1 Administ 없음          921 Jan 20 12:49 ./doc/nesdoc/cricket/apps.Blink.SingleTimer.
nc.text.html
-rw-r--r--    1 Administ 없음         3987 Jan 20 12:49 ./doc/nesdoc/cricket/tos.interfaces.Timer.nc
.html
lrwxrwxrwx    1 Administ 없음          174 Jan 20 12:49 ./doc/nesdoc/cricket/tos.interfaces.Timer.nc
.source -> /opt/tinyos-1.x/tos/interfaces/Timer.nc
-rw-r--r--    1 Administ 없음         3610 Jan 20 12:49 ./doc/nesdoc/cricket/tos.platform.cricket.Ti
merM.nc.html
lrwxrwxrwx    1 Administ 없음          188 Jan 20 12:49 ./doc/nesdoc/cricket/tos.platform.cricket.Ti
merM.nc.source -> /opt/tinyos-1.x/tos/platform/cricket/TimerM.nc
-rw-r--r--    1 Administ 없음         4036 Jan 20 12:49 ./doc/nesdoc/cricket/tos.system.TimerC.nc.ht
ml
-rw-r--r--    1 Administ 없음         1296 Jan 20 12:49 ./doc/nesdoc/cricket/tos.system.TimerC.nc.if
.dot
-rwxr-xr-x    1 Administ 없음         7813 Jan 20 12:49 ./doc/nesdoc/cricket/tos.system.TimerC.nc.if
.gif
lrwxrwxrwx    1 Administ 없음          168 Jan 20 12:49 ./doc/nesdoc/cricket/tos.system.TimerC.nc.so
urce -> /opt/tinyos-1.x/tos/system/TimerC.nc
-rw-r--r--    1 Administ 없음         1908 Jan 20 12:49 ./doc/nesdoc/cricket/tos.system.TimerC.nc.te
xt.html
-rw-r--r--    1 Administ 없음         1639 Oct  8  2003 ./tos/interfaces/AbsoluteTimer.h
-rw-r--r--    1 Administ 없음         2163 Oct  8  2003 ./tos/interfaces/AbsoluteTimer.nc
-rw-r--r--    1 Administ 없음         1730 Oct  8  2003 ./tos/interfaces/Timer.h
-rw-r--r--    1 Administ 없음         3046 Oct  8  2003 ./tos/interfaces/Timer.nc
-rwx------    1 Administ 없음         8524 Oct 27  2004 ./tos/platform/cricket/TimerM.nc
-rw-r--r--    1 Administ 없음         2260 Oct  8  2003 ./tos/platform/pc/TimerC.nc
-rw-r--r--    1 Administ 없음         2260 Oct  8  2003 ./tos/system/TimerC.nc
-rw-r--r--    1 Administ 없음         6120 Apr 15  2004 ./tos/system/TimerM.nc

$ find . -name '*Timer*' -exec ls -al {} \;
위 명령어를 분석해 보자.
find : 파일을 찾겠다라는 명령어이다.

. : 현재폴더와 하위폴더 모두 찾겠다 라는 뜻이된다.

-name : 파일이름을 무엇으로 찾겠다 라고 명시하는 옵션이 된다.

'*Timer*' : 이것은 *이라는것은 아무 문자나 들어올수 있다는뜻이 되므로 Timer라는 문자 앞뒤에 어떤한 문자열이 붙은 파일을 찾겠다 라는것이된다.

-exec : 검색조건이 맞으면 명령을 실행하라는 옵션으로 . 끝은 \;로 끝내고 find가 검색한것을 인수로 사용하고 싶다면 {}를 사용하면된다.


예제 2)
찾은 파일에 출력옵션을 주어 자신이 원하는 출력만 하게 해보았다.
출력 옵션

형식 : <경로/파일명> <파일크기KB> ex) "%h/%f \t %kKB \n"

%h = 경로명
%f = 파일명
%k = KB로 표기
%s = Bytes로 표기


$ find . -name '*Timer*' -printf "%f \n"
SingleTimer.nc
SingleTimer.nc
MicroTimer.nc
MicroTimerM.nc
apps.Blink.SingleTimer.nc.html
apps.Blink.SingleTimer.nc.if.dot
apps.Blink.SingleTimer.nc.if.gif
apps.Blink.SingleTimer.nc.source
apps.Blink.SingleTimer.nc.text.html
tos.interfaces.Timer.nc.html
tos.interfaces.Timer.nc.source
tos.platform.cricket.TimerM.nc.html
tos.platform.cricket.TimerM.nc.source
tos.system.TimerC.nc.html
tos.system.TimerC.nc.if.dot
tos.system.TimerC.nc.if.gif
tos.system.TimerC.nc.source
tos.system.TimerC.nc.text.html
AbsoluteTimer.h
AbsoluteTimer.nc
Timer.h
Timer.nc
TimerM.nc
TimerC.nc
TimerC.nc
TimerM.nc


find . -maxdepth 1 -name "s*" -type d -exec cp input.txt {} \;

현재 폴더에서 s로 시작하는 폴더만 찾아서 모든 폴더 안에 input.txt를 넣는 방법 ^_^

저작자 표시 비영리 동일 조건 변경 허락
Posted by Lws