Linux2010. 2. 26. 20:55
find는 기본적으로 하위 디렉토리를 뒤지도록 되어있기 때문에
강제로 현재 디렉토리만 검색을 하기 위해서는 옵션을 해주어야 한다.

아쉽게도 non-recursive 이런 옵션은 없고
-maxdepth 숫자
라는 옵션이 존재한다.

예를 들어 현재 디렉토리에서 크기가 0인 파일을 찾기 위해서는
$ find ./ -maxdepth 1 -size 0c
이라고 하면된다.

find: warning: you have specified the -maxdepth option after a non-option argument -size, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

maxdepth는 옵션이고
size는 옵션이 아니므로 순서를 반대로 하면 경고를 발생시킨다.



-maxdepth levels
    Descend at most levels (a non-negative integer) levels of directories below the command line arguments. '-maxdepth 0' means only apply the tests and actions to the command line arguments.

[링크 : http://linux.die.net/man/1/find]

[링크 : http://www.unix.com/unix-dummies-questions-answers/423-using-find-command-only-current-directory.html]
Posted by 구차니
일반적으로 Ctrl-R 옵션이나 Ctrl-H 옵션등으로
디렉토리 내의 모든 파일의 내용을 치환하는 것이 있다.

find ./ -exec grep "content" {} \; -exec sed -i "s/content/container/g" {} \;

[링크 : http://www.linux-forum.de/find-grep-replace-string-33557.html]

아무튼 위의 경우는 grep에 옵션이 들어가지 않았고, 모든 파일에 대해서 검색을 하므로
ctags 사용중에는 tags 파일까지 검색을 하게 된다.

크로스컴파일 목적으로 CC 라던가 LD 등을 고치려고 하면

find ./ -name Makefile -exec grep -w "= gcc" {} \; -exec sed -i "s/\= gcc/\= sh4-linux-gcc/g" {} \;

이렇게 find에 -name 옵션으로 파일을 지정하고(makefile 혹은 Makefile)
grep 옵션에 -w (Whole word matching) 으로 한정을 해주면 된다.

2010.02.04 추가
위의 예제는 "= gcc" 라고 들어있는 부분을 전체가 포함된 내용을 찾아 "= sh4-linux-gcc"로 변경해주는 것이다.
예를 들어 "= gcc3" 이런식으로 된 곳은  grep -w 옵션에 의해 무시된다.
그리고 tags 파일을 피하기 위해 find ./ -name Makefile 로 makefile에서만 수정을 하도록 제한한다.

2010/02/01 - [Linux] - grep에서 정확하게 찾기

Posted by 구차니
Linux2008. 12. 26. 16:18
find 라는 명령어는 -exec 라는 옵션을 사용하여 파이프라인을 통해 검색된 결과를 넘겨 줄 수 있다.

윈도우/리눅스를 혼합하여 사용할 경우 의미 없이 용량만 늘리는 Thumbs.db 라는 넘을 삭제 하고 싶은데
일일이 찾아 지우기는 귀찮으니 조금 더 귀차니즘을 발휘하여 find에게 떠밀어 보자

 find [start path] -name Thumbs.db -exec rm {} \;

{} 는 검색된 결과가 넘어 가는 것이고
\; 는 명령어의 끝을 나타낸다. {}는 붙여 써야 하고(치환자)
\;역시 붙여 써야 하되, {} \;는 띄워서 써야 한다.


[참고 : http://kin.naver.com/detail/detail.php?d1id=1&dir_id=10202&eid=uOUzGMEfvG/Z2+VmcTxiS3R/sb6P15T+&qb=ZmluZCBleGVj&pid=fSfZswoi5URssbZwiR4sss--173216&sid=SVSD7S1zVEkAACzVjnk]
Posted by 구차니
Linux2008. 12. 19. 20:05
man find

       -type c
              File is of type c:

              b      block (buffered) special

              c      character (unbuffered) special

              d      directory

              p      named pipe (FIFO)

              f      regular file

              l      symbolic link; this is never true if the -L option or the -follow option is in effect,
                     unless  the symbolic link is broken.  If you want to search for symbolic links when -L
                     is in effect, use -xtype.

              s      socket

              D      door (Solaris)


'ls -l'을  실행하면
$ ll
total 1922
drwxr-xr-x   2 root root    4096 Nov 15 04:17 bin
drwxr-xr-x   4 root root    1024 Apr 11  2008 boot
drwxr-xr-x  12 root root    3900 Nov 14 13:07 dev
drwxr-xr-x 101 root root   12288 Dec 15 04:15 etc
drwxr-xr-x  10 root root    4096 Aug 19 13:59 home
drwxr-xr-x  14 root root    4096 Apr 11  2008 lib
drwx------   2 root root   16384 Apr 10  2008 lost+found
drwxr-xr-x   2 root root    4096 Nov 14 13:07 media
drwxr-xr-x   2 root root       0 Nov 14 13:07 misc
drwxr-xr-x   2 root root       0 Nov 14 13:07 mnt
drwxr-xr-x   2 root root       0 Nov 14 13:07 net
drwxr-xr-x   2 root root    4096 Jun 19  2008 nfsroot
drwxr-xr-x   3 root root    4096 May 29  2008 opt
dr-xr-xr-x 195 root root       0 Nov 14 13:06 proc
drwxr-x---  29 root root    4096 Dec 17 11:00 root
drwxr-xr-x   2 root root   12288 Apr 11  2008 sbin
drwxr-xr-x   2 root root    4096 Apr 10  2008 selinux
drwxr-xr-x   2 root root    4096 Oct 11  2006 srv
drwxr-xr-x  12 root root       0 Nov 14 13:06 sys
drwxr-xr-x   2 root root    4096 Dec 17 13:30 tftpboot
drwxrwxrwt  16 root dev    90112 Dec 19 20:01 tmp
drwxr-xr-x  15 root root    4096 May 30  2008 usr
drwxr-xr-x  24 root root    4096 Apr 10  2008 var

이런식으로 퍼미션과 파일의 종류가 출력이 된다.
아무튼 일반적으로 보이는 녀석들은

- 일반 파일
b Block device
c Character device
d Directory
l symbolic Link

인데 드물게 보이는 녀석들이 존재 한다.
prw------- 1 morpheuz dev        0 Jul 15 13:21 initctl
crw-rw---- 1 morpheuz dev  61,   0 May 30  2008 lirc
srwxr-xr-x 1 morpheuz dev        0 Oct 20 19:57 lircd
brw-rw---- 1 morpheuz dev   7,   0 May 30  2008 loop0

이 녀석들의 의미는 역설적이게도.. find에서 찾아 냈다.

p name Pipe
s Socket


Posted by 구차니